CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-cdap-cdap--cdap-common

CDAP Common provides core common utilities and abstractions for the CDAP (Cask Data Application Platform) ecosystem including exception handling, service management, configuration, HTTP utilities, metadata management, security abstractions, discovery services, and various utility classes that are shared across CDAP components.

Pending
Overview
Eval results
Files

utilities.mddocs/

Utility Classes

Collection of utility classes for common operations including file handling, string manipulation, time parsing, hash calculations, and system detection.

Capabilities

File and Directory Utilities

Utilities for file system operations and directory management.

/**
 * File system utilities
 */
public final class FileUtils {
    /**
     * Ensure directory exists, creating it if necessary
     */
    public static void ensureDirectoryExists(File directory) throws IOException;
    
    /**
     * Delete directory contents recursively
     */
    public static void deleteDirectoryContents(File directory) throws IOException;
    
    /**
     * Copy file from source to destination
     */
    public static void copy(File source, File destination) throws IOException;
    
    /**
     * Move file from source to destination
     */
    public static void move(File source, File destination) throws IOException;
    
    /**
     * Get file extension
     */
    public static String getExtension(File file);
    
    /**
     * Check if file is readable
     */
    public static boolean isReadable(File file);
    
    /**
     * Get file size in bytes
     */
    public static long getFileSize(File file);
}

/**
 * Directory operation utilities
 */
public class DirUtils {
    /**
     * Create temporary directory
     */
    public static File createTempDir(String prefix) throws IOException;
    
    /**
     * List files in directory with filter
     */
    public static List<File> listFiles(File directory, FileFilter filter);
    
    /**
     * Get directory size recursively
     */
    public static long getDirectorySize(File directory);
    
    /**
     * Clean directory by removing old files
     */
    public static void cleanDirectory(File directory, long maxAgeMs);
}

String Manipulation Utilities

Utilities for string processing and manipulation.

/**
 * String manipulation utilities
 */
public class StringUtils {
    /**
     * Check if string is null or empty
     */
    public static boolean isEmpty(String str);
    
    /**
     * Check if string is not null and not empty
     */
    public static boolean isNotEmpty(String str);
    
    /**
     * Join array of strings with delimiter
     */
    public static String join(String[] parts, String delimiter);
    
    /**
     * Join collection of strings with delimiter
     */
    public static String join(Collection<String> parts, String delimiter);
    
    /**
     * Split string and trim whitespace from parts
     */
    public static String[] splitAndTrim(String str, String delimiter);
    
    /**
     * Escape special characters for regex
     */
    public static String escapeRegex(String str);
    
    /**
     * Convert camelCase to snake_case
     */
    public static String camelToSnake(String camelCase);
    
    /**
     * Convert snake_case to camelCase
     */
    public static String snakeToCamel(String snakeCase);
}

Time and Date Utilities

Utilities for time parsing, calculation, and formatting.

/**
 * Time expression parser for relative time calculations
 */
public class TimeMathParser {
    /**
     * Parse time math expression to milliseconds
     * Examples: "5m", "2h", "1d", "now-1h", "now+30m"
     */
    public static long parseTimeInMs(String timeMath) throws IllegalArgumentException;
    
    /**
     * Parse time math expression to seconds
     */
    public static long parseTimeInSeconds(String timeMath) throws IllegalArgumentException;
    
    /**
     * Parse relative time from base timestamp
     */
    public static long parseRelativeTime(String timeMath, long baseTime) throws IllegalArgumentException;
    
    /**
     * Check if string is a valid time expression
     */
    public static boolean isValidTimeExpression(String expr);
}

/**
 * Time provider interface for testability
 */
public interface TimeProvider {
    /**
     * Get current time in milliseconds
     */
    long currentTimeMillis();
}

/**
 * System time provider (default implementation)
 */
public class SystemTimeProvider implements TimeProvider {
    @Override
    public long currentTimeMillis();
}

Hash and Checksum Utilities

Utilities for hash calculations and data integrity checks.

/**
 * Hash calculation utilities
 */
public class HashUtils {
    /**
     * Calculate MD5 hash of byte array
     */
    public static String md5(byte[] data);
    
    /**
     * Calculate MD5 hash of string
     */
    public static String md5(String data);
    
    /**
     * Calculate SHA-1 hash of byte array
     */
    public static String sha1(byte[] data);
    
    /**
     * Calculate SHA-256 hash of byte array
     */
    public static String sha256(byte[] data);
    
    /**
     * Calculate hash of file
     */
    public static String fileHash(File file, String algorithm) throws IOException;
}

/**
 * Checksum calculation utilities
 */
public class Checksums {
    /**
     * Calculate CRC32 checksum
     */
    public static long crc32(byte[] data);
    
    /**
     * Calculate CRC32 checksum of file
     */
    public static long crc32(File file) throws IOException;
    
    /**
     * Calculate Adler32 checksum
     */
    public static long adler32(byte[] data);
    
    /**
     * Verify checksum matches expected value
     */
    public static boolean verify(byte[] data, long expectedChecksum, String algorithm);
}

System Detection Utilities

Utilities for detecting system properties and capabilities.

/**
 * Operating system detection utility
 */
public class OSDetector {
    /**
     * Check if running on Windows
     */
    public static boolean isWindows();
    
    /**
     * Check if running on Linux
     */
    public static boolean isLinux();
    
    /**
     * Check if running on macOS
     */
    public static boolean isMac();
    
    /**
     * Get operating system name
     */
    public static String getOSName();
    
    /**
     * Get operating system architecture
     */
    public static String getArchitecture();
    
    /**
     * Check if running on 64-bit architecture
     */
    public static boolean is64Bit();
}

/**
 * Port availability detection utility
 */
public class PortDetector {
    /**
     * Check if port is available
     */
    public static boolean isPortAvailable(int port);
    
    /**
     * Check if port is available on specific host
     */
    public static boolean isPortAvailable(String host, int port);
    
    /**
     * Find next available port starting from given port
     */
    public static int findAvailablePort(int startPort);
    
    /**
     * Find N available ports
     */
    public static List<Integer> findAvailablePorts(int count, int startPort);
}

/**
 * Project information utilities
 */
public class ProjectInfo {
    /**
     * Get project version from manifest
     */
    public static String getVersion();
    
    /**
     * Get project version for specific class
     */
    public static String getVersion(Class<?> clazz);
    
    /**
     * Get build timestamp
     */
    public static String getBuildTime();
    
    /**
     * Get git commit hash
     */
    public static String getGitCommit();
}

Run ID Generation

Utilities for generating unique program run identifiers using time-based UUIDs.

/**
 * Utilities for generating unique run IDs for CDAP program runs
 */
public final class RunIds {
    /**
     * Generate a new run ID based on current time
     * @return Time-based UUID run ID
     */
    public static RunId generate();
    
    /**
     * Generate run ID for specific time (testing purposes)
     * @param timeMillis Timestamp in milliseconds
     * @return Time-based UUID run ID for the specified time
     */
    public static RunId generate(long timeMillis);
    
    /**
     * Convert string representation to RunId
     * @param id String representation of UUID
     * @return RunId object
     */
    public static RunId fromString(String id);
    
    /**
     * Extract timestamp from run ID
     * @param runId The run ID to extract time from
     * @param timeUnit Time unit for the result
     * @return Timestamp in specified time unit
     */
    public static long getTime(RunId runId, TimeUnit timeUnit);
    
    /**
     * Extract timestamp from run ID string
     * @param runId The run ID string to extract time from  
     * @param timeUnit Time unit for the result
     * @return Timestamp in specified time unit
     */
    public static long getTime(String runId, TimeUnit timeUnit);
    
    /**
     * JSON codec for RunId serialization/deserialization
     */
    public static class RunIdCodec implements JsonSerializer<RunId>, JsonDeserializer<RunId> {
        @Override
        public JsonElement serialize(RunId src, Type typeOfSrc, JsonSerializationContext context);
        
        @Override
        public RunId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context);
    }
}

Usage Examples:

import io.cdap.cdap.common.app.RunIds;
import java.util.concurrent.TimeUnit;

// Generate run IDs
RunId currentRunId = RunIds.generate();
System.out.println("Current run ID: " + currentRunId.getId());

// Generate run ID for specific time (useful for testing)
long specificTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1);
RunId historicRunId = RunIds.generate(specificTime);

// Convert between string and RunId
String runIdString = currentRunId.getId();
RunId parsedRunId = RunIds.fromString(runIdString);

// Extract timestamps from run IDs
long runTimeMs = RunIds.getTime(currentRunId, TimeUnit.MILLISECONDS);
long runTimeSeconds = RunIds.getTime(runIdString, TimeUnit.SECONDS);

System.out.println("Run started at: " + new Date(runTimeMs));
System.out.println("Run started " + 
    (System.currentTimeMillis() - runTimeMs) / 1000 + " seconds ago");

Task Execution Utilities

Utilities for task execution and coordination.

/**
 * Task execution utilities
 */
public class Tasks {
    /**
     * Execute task with timeout
     */
    public static <T> T execute(Callable<T> task, long timeout, TimeUnit unit) 
        throws Exception, TimeoutException;
    
    /**
     * Execute runnable with timeout
     */
    public static void execute(Runnable task, long timeout, TimeUnit unit) 
        throws TimeoutException;
    
    /**
     * Execute task with retry
     */
    public static <T> T executeWithRetry(Callable<T> task, int maxAttempts, 
                                        long retryDelay, TimeUnit unit) throws Exception;
    
    /**
     * Execute multiple tasks in parallel
     */
    public static <T> List<T> executeParallel(List<Callable<T>> tasks, 
                                             ExecutorService executor) throws Exception;
    
    /**
     * Execute task periodically
     */
    public static ScheduledFuture<?> executePeriodically(Runnable task, 
                                                        long initialDelay, long period, 
                                                        TimeUnit unit, 
                                                        ScheduledExecutorService executor);
}

Usage Examples:

import io.cdap.cdap.common.utils.*;
import java.util.concurrent.*;

// File operations
public class FileOperations {
    public void setupWorkspace(String workspaceRoot) throws IOException {
        File workspace = new File(workspaceRoot);
        
        // Ensure workspace directory exists
        FileUtils.ensureDirectoryExists(workspace);
        
        // Create subdirectories
        File dataDir = new File(workspace, "data");
        File logsDir = new File(workspace, "logs");
        File tempDir = new File(workspace, "temp");
        
        FileUtils.ensureDirectoryExists(dataDir);
        FileUtils.ensureDirectoryExists(logsDir);
        FileUtils.ensureDirectoryExists(tempDir);
        
        // Clean old temporary files (older than 1 hour)
        DirUtils.cleanDirectory(tempDir, TimeUnit.HOURS.toMillis(1));
        
        System.out.println("Workspace setup complete. Size: " + 
                          DirUtils.getDirectorySize(workspace) + " bytes");
    }
    
    public void processFiles(File inputDir, File outputDir) throws IOException {
        // List only .txt files
        List<File> textFiles = DirUtils.listFiles(inputDir, 
            file -> file.getName().endsWith(".txt"));
        
        for (File file : textFiles) {
            if (FileUtils.isReadable(file)) {
                File destination = new File(outputDir, 
                    "processed_" + file.getName());
                FileUtils.copy(file, destination);
                
                System.out.println("Processed: " + file.getName() + 
                                  " (" + FileUtils.getFileSize(file) + " bytes)");
            }
        }
    }
}

// String processing
public class DataProcessor {
    public void processConfigValues(Properties config) {
        for (String key : config.stringPropertyNames()) {
            String value = config.getProperty(key);
            
            if (StringUtils.isEmpty(value)) {
                System.out.println("Empty value for key: " + key);
                continue;
            }
            
            // Convert configuration keys from camelCase to snake_case
            String normalizedKey = StringUtils.camelToSnake(key);
            
            // Split comma-separated values and trim
            if (value.contains(",")) {
                String[] parts = StringUtils.splitAndTrim(value, ",");
                String joined = StringUtils.join(parts, " | ");
                System.out.println(normalizedKey + " = " + joined);
            }
        }
    }
}

// Time-based operations
public class TimeBasedService {
    private final TimeProvider timeProvider = new SystemTimeProvider();
    
    public void scheduleCleanup(String retentionPeriod) {
        try {
            // Parse retention period (e.g., "7d", "24h", "30m")
            long retentionMs = TimeMathParser.parseTimeInMs(retentionPeriod);
            long cutoffTime = timeProvider.currentTimeMillis() - retentionMs;
            
            System.out.println("Cleaning up files older than: " + 
                              new Date(cutoffTime));
            
            // Cleanup logic...
            cleanupOldFiles(cutoffTime);
            
        } catch (IllegalArgumentException e) {
            System.err.println("Invalid retention period: " + retentionPeriod);
        }
    }
    
    public void scheduleRelativeExecution(String timeExpression) {
        try {
            // Parse relative time (e.g., "now+5m", "now-1h")
            long executionTime = TimeMathParser.parseTimeInMs(timeExpression);
            long delay = executionTime - timeProvider.currentTimeMillis();
            
            if (delay > 0) {
                // Schedule future execution
                scheduleExecution(delay);
            } else {
                System.out.println("Time expression refers to past: " + timeExpression);
            }
            
        } catch (IllegalArgumentException e) {
            System.err.println("Invalid time expression: " + timeExpression);
        }
    }
    
    private void cleanupOldFiles(long cutoffTime) {
        // Implementation...
    }
    
    private void scheduleExecution(long delay) {
        // Implementation...
    }
}

// System detection and adaptation
public class PlatformService {
    public void configurePlatformSpecific() {
        System.out.println("Platform: " + OSDetector.getOSName() + 
                          " (" + OSDetector.getArchitecture() + ")");
        
        if (OSDetector.isWindows()) {
            configureWindows();
        } else if (OSDetector.isLinux()) {
            configureLinux();
        } else if (OSDetector.isMac()) {
            configureMac();
        }
        
        // Find available ports for services
        List<Integer> ports = PortDetector.findAvailablePorts(3, 8080);
        System.out.println("Available ports: " + ports);
    }
    
    public void validateSystemRequirements() {
        if (!OSDetector.is64Bit()) {
            throw new RuntimeException("64-bit architecture required");
        }
        
        if (!PortDetector.isPortAvailable(8080)) {
            System.out.println("Port 8080 not available, finding alternative...");
            int alternativePort = PortDetector.findAvailablePort(8081);
            System.out.println("Using port: " + alternativePort);
        }
    }
    
    private void configureWindows() { /* Windows-specific config */ }
    private void configureLinux() { /* Linux-specific config */ }
    private void configureMac() { /* macOS-specific config */ }
}

// Task execution patterns
public class TaskExecutionService {
    private final ExecutorService executor = Executors.newFixedThreadPool(10);
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
    
    public void processWithTimeout() {
        try {
            String result = Tasks.execute(() -> {
                // Long-running operation
                Thread.sleep(5000);
                return "Operation completed";
            }, 10, TimeUnit.SECONDS);
            
            System.out.println("Result: " + result);
            
        } catch (TimeoutException e) {
            System.err.println("Operation timed out");
        } catch (Exception e) {
            System.err.println("Operation failed: " + e.getMessage());
        }
    }
    
    public void processWithRetry() {
        try {
            String result = Tasks.executeWithRetry(() -> {
                // Operation that might fail
                if (Math.random() < 0.7) {
                    throw new RuntimeException("Random failure");
                }
                return "Success";
            }, 3, 1000, TimeUnit.MILLISECONDS);
            
            System.out.println("Result after retries: " + result);
            
        } catch (Exception e) {
            System.err.println("All retry attempts failed: " + e.getMessage());
        }
    }
    
    public void processInParallel() {
        List<Callable<String>> tasks = Arrays.asList(
            () -> "Task 1 result",
            () -> "Task 2 result", 
            () -> "Task 3 result"
        );
        
        try {
            List<String> results = Tasks.executeParallel(tasks, executor);
            System.out.println("Parallel results: " + results);
            
        } catch (Exception e) {
            System.err.println("Parallel execution failed: " + e.getMessage());
        }
    }
    
    public void schedulePeriodicTask() {
        ScheduledFuture<?> future = Tasks.executePeriodically(
            () -> System.out.println("Periodic task executed at: " + new Date()),
            0, 30, TimeUnit.SECONDS, scheduler
        );
        
        // Cancel after 5 minutes
        scheduler.schedule(() -> future.cancel(false), 5, TimeUnit.MINUTES);
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-io-cdap-cdap--cdap-common

docs

configuration.md

exceptions.md

http.md

index.md

io.md

logging.md

network.md

security.md

services.md

utilities.md

tile.json