├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── lib └── README.md ├── src └── main │ ├── resources │ ├── listClasses.txt │ ├── import.txt │ ├── export.txt │ ├── gensrc.txt │ └── help.txt │ └── java │ └── es │ └── litesolutions │ └── cache │ ├── db │ ├── CacheQueryProvider.java │ ├── CacheSqlQuery.java │ └── CacheDb.java │ ├── commands │ ├── ListClassesCommand.java │ ├── GensrcCommand.java │ ├── CachedbCommand.java │ ├── ExportCommand.java │ └── ImportCommand.java │ ├── CacheImport.java │ ├── Util.java │ └── CacheRunner.java ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | target 3 | .idea 4 | build 5 | .gradle 6 | out 7 | classes 8 | lib 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/intersystems-community/cachedb-import/master/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /lib/README.md: -------------------------------------------------------------------------------- 1 | In this directory, you should copy the `cachejdbc.jar` and `cachedb.jar` of your 2 | Caché installation. 3 | 4 | Note that both jars for JDK 1.7 and 1.8 will work. 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Feb 10 09:00:58 CET 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=http\://services.gradle.org/distributions/gradle-2.11-all.zip 7 | -------------------------------------------------------------------------------- /src/main/resources/listClasses.txt: -------------------------------------------------------------------------------- 1 | Syntax: 2 | 3 | java listClasses 4 | 5 | This command lists all classes for the specified database, namespace, user and 6 | password, except system classes. 7 | 8 | See: 9 | 10 | java help 11 | 12 | for a list of global arguments. 13 | 14 | Arguments: 15 | 16 | NONE 17 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/db/CacheQueryProvider.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.db; 2 | 3 | import com.intersys.objects.CacheException; 4 | import com.intersys.objects.CacheQuery; 5 | import com.intersys.objects.Database; 6 | 7 | /** 8 | * Functional interface for {@link CacheDb#query(CacheQueryProvider)} 9 | */ 10 | @FunctionalInterface 11 | public interface CacheQueryProvider 12 | { 13 | CacheQuery getQuery(Database db) 14 | throws CacheException; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/resources/import.txt: -------------------------------------------------------------------------------- 1 | Syntax: 2 | 3 | java import [arguments] 4 | 5 | This command imports an XML export into the specified database and namespace, 6 | with the specified user and password. 7 | 8 | The XML file is expected to be encoded in UTF-8. 9 | 10 | See: 11 | 12 | java help 13 | 14 | for a list of global arguments. 15 | 16 | Arguments: 17 | 18 | * inputFile=the.xml [MANDATORY] 19 | 20 | Path to the XML file to be imported. 21 | 22 | * mode=[file|stream] [OPTIONAL] 23 | 24 | Tells which mode to use to import. The default is file. If file does 25 | not work for you, try stream. 26 | -------------------------------------------------------------------------------- /src/main/resources/export.txt: -------------------------------------------------------------------------------- 1 | Syntax: 2 | 3 | java export [arguments] 4 | 5 | This command exports all classes (except system classes) of a given namespace. 6 | The files will be written in UTF-8. 7 | 8 | See: 9 | 10 | java help 11 | 12 | for a list of global arguments. 13 | 14 | Arguments: 15 | 16 | * outputDir=someDir [MANDATORY] 17 | 18 | Tells where the output files should be written. The directory must not 19 | exist, except if the overwrite option is true. 20 | 21 | * overwrite= [OPTIONAL] 22 | 23 | Tells what should happen if the directory already exists. The default is 24 | false. 25 | 26 | If true, the directory will be deleted recursively before being created 27 | again. 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/commands/ListClassesCommand.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.commands; 2 | 3 | import com.intersys.objects.CacheException; 4 | import es.litesolutions.cache.db.CacheDb; 5 | 6 | import java.sql.SQLException; 7 | import java.util.Map; 8 | import java.util.Set; 9 | 10 | /** 11 | * ListClasses command: list COS classes from a database 12 | */ 13 | public final class ListClassesCommand 14 | extends CachedbCommand 15 | { 16 | public ListClassesCommand(final CacheDb db, 17 | final Map arguments) 18 | { 19 | super(db, arguments); 20 | } 21 | 22 | @Override 23 | public void execute() 24 | throws CacheException, SQLException 25 | { 26 | final Set set = runner.listClasses(includeSys); 27 | set.stream().sorted().forEach(System.out::println); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/db/CacheSqlQuery.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.db; 2 | 3 | import com.intersys.objects.CacheException; 4 | import com.intersys.objects.CacheQuery; 5 | import com.intersys.objects.CacheServerException; 6 | 7 | import java.sql.ResultSet; 8 | 9 | /** 10 | * An {@link AutoCloseable} wrapper over a {@link CacheQuery} 11 | */ 12 | public final class CacheSqlQuery 13 | implements AutoCloseable 14 | { 15 | private final CacheQuery query; 16 | 17 | /** 18 | * Constructor 19 | * 20 | * @param query the query 21 | */ 22 | public CacheSqlQuery(final CacheQuery query) 23 | { 24 | this.query = query; 25 | } 26 | 27 | /** 28 | * Get the JDBC {@link ResultSet} from this query 29 | * 30 | * @return the result set 31 | * @throws CacheException unable to obtain the result set 32 | */ 33 | public ResultSet execute() 34 | throws CacheException 35 | { 36 | return query.execute(); 37 | } 38 | 39 | @Override 40 | public void close() 41 | throws CacheServerException 42 | { 43 | query.close(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/commands/GensrcCommand.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.commands; 2 | 3 | import com.intersys.objects.CacheException; 4 | import es.litesolutions.cache.db.CacheDb; 5 | 6 | import java.io.IOException; 7 | import java.sql.SQLException; 8 | import java.util.Map; 9 | import java.util.Set; 10 | 11 | /** 12 | * Gensrc command: import into the database, then read back as source 13 | * 14 | *

In fact, this command makes use of both an {@link ImportCommand} and an 15 | * {@link ExportCommand}.

16 | */ 17 | public final class GensrcCommand 18 | extends CachedbCommand 19 | { 20 | private final ImportCommand importCommand; 21 | private final ExportCommand exportCommand; 22 | 23 | public GensrcCommand(final CacheDb cacheDb, 24 | final Map arguments) 25 | { 26 | super(cacheDb, arguments); 27 | importCommand = new ImportCommand(cacheDb, arguments); 28 | exportCommand = new ExportCommand(cacheDb, arguments); 29 | } 30 | 31 | @Override 32 | public void execute() 33 | throws IOException, CacheException, SQLException 34 | { 35 | final Set classes = importCommand.importAndList(); 36 | exportCommand.prepareDirectory(); 37 | exportCommand.writeClasses(classes); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/resources/gensrc.txt: -------------------------------------------------------------------------------- 1 | Syntax: 2 | 3 | java gensrc [arguments] 4 | 5 | This command first imports all classes from an XML export into a Caché instance, 6 | then exports them as sources in a given directory. It can be viewed as a 7 | combination of both the import and export command. 8 | 9 | See: 10 | 11 | java help 12 | 13 | for a list of global arguments. 14 | 15 | Arguments: 16 | 17 | * inputFile=the.xml [MANDATORY] 18 | 19 | Path to the XML file to be imported. 20 | 21 | * mode=[file|stream] [OPTIONAL] 22 | 23 | Tells which mode to use to import. The default is file. If file does 24 | not work for you, try stream. 25 | 26 | Note: if stream is chosen, the list of exported classes is a heuristic. 27 | First, the list of classes is collected before import, then computed 28 | again after import. This is due to the fact that the stream mode is 29 | unable to return a list of imported classes, unlike the file mode. 30 | 31 | * outputDir=someDir [MANDATORY] 32 | 33 | Tells where the output files should be written. The directory must not 34 | exist, except if the overwrite option is true. 35 | 36 | * overwrite= [OPTIONAL] 37 | 38 | Tells what should happen if the directory already exists. The default is 39 | false. 40 | 41 | If true, the directory will be deleted recursively before being created 42 | again. 43 | 44 | -------------------------------------------------------------------------------- /src/main/resources/help.txt: -------------------------------------------------------------------------------- 1 | Syntax: 2 | 3 | java [arguments] 4 | java help 5 | java help 6 | 7 | The command argument is mandatory. 8 | 9 | Available commands are: listClasses, export, import, gensrc. 10 | 11 | Arguments common to all commands: 12 | 13 | * cfg=somefile [OPTIONAL] 14 | 15 | Read arguments from "somefile", which must be a Java properties file 16 | in UTF-8. Note that arguments on the command line take precedence. 17 | 18 | * verbose= [OPTIONAL] 19 | 20 | Augment the verbosity level of the program. The default is false. 21 | 22 | * host=dbhost [OPTIONAL] 23 | 24 | Specify the host where the Caché installation is hosted. If not 25 | specified, the default is localhost. 26 | 27 | * port=dbport [OPTIONAL] 28 | 29 | Specify the port to connect to. If not specified, the default is 1972. 30 | 31 | * user=dbuser [MANDATORY] 32 | 33 | Specify the user with which to connect to the database. 34 | 35 | * password=dbpassword [MANDATORY] 36 | 37 | Specify the password for the Caché database user. 38 | 39 | * namespace=somenamespace [MANDATORY] 40 | 41 | Specify the namespace this user must try and connect to. 42 | 43 | * includeSys=[true|false] [OPTIONAL] 44 | 45 | Also import/list/export "sysem" classes; that is, classes whose name 46 | starts with a % sign. 47 | 48 | The default is false. 49 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/commands/CachedbCommand.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.commands; 2 | 3 | import es.litesolutions.cache.CacheImport; 4 | import es.litesolutions.cache.db.CacheDb; 5 | import es.litesolutions.cache.CacheRunner; 6 | import es.litesolutions.cache.Util; 7 | 8 | import java.util.Map; 9 | 10 | /** 11 | * Abstract class for a command run by {@link CacheImport} 12 | */ 13 | public abstract class CachedbCommand 14 | { 15 | protected static final String INCLUDESYS = "includeSys"; 16 | protected static final String INCLUDESYS_DEFAULT = "false"; 17 | 18 | protected final CacheDb cacheDb; 19 | protected final CacheRunner runner; 20 | protected final Map arguments; 21 | 22 | protected final boolean includeSys; 23 | 24 | /** 25 | * Constructor 26 | * 27 | * @param cacheDb the database 28 | * @param arguments map of arguments 29 | */ 30 | @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter") 31 | protected CachedbCommand(final CacheDb cacheDb, 32 | final Map arguments) 33 | { 34 | this.cacheDb = cacheDb; 35 | runner = new CacheRunner(cacheDb); 36 | this.arguments = arguments; 37 | 38 | includeSys = Boolean.parseBoolean(Util.readOrDefault(INCLUDESYS, 39 | arguments, INCLUDESYS_DEFAULT)); 40 | } 41 | 42 | /** 43 | * Execute this command 44 | * 45 | * @throws Exception refined in each individual command 46 | */ 47 | public abstract void execute() 48 | throws Exception; 49 | 50 | protected final String getArgument(final String name) 51 | { 52 | return Util.readArgument(name, arguments); 53 | } 54 | 55 | protected final String getArgumentOrDefault(final String name, 56 | final String defaultValue) 57 | { 58 | return Util.readOrDefault(name, arguments, defaultValue); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/db/CacheDb.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.db; 2 | 3 | import com.intersys.cache.Dataholder; 4 | import com.intersys.classes.Dictionary.ClassDefinition; 5 | import com.intersys.objects.CacheDatabase; 6 | import com.intersys.objects.CacheException; 7 | import com.intersys.objects.CacheQuery; 8 | import com.intersys.objects.Database; 9 | 10 | /** 11 | * Wrapper over a Caché {@link Database} 12 | * 13 | *

The only reason for the existence of this class is that the class provided 14 | * by InterSystems does not implement {@link AutoCloseable}; this class does. 15 | *

16 | * 17 | * @see Database#close() 18 | */ 19 | public final class CacheDb 20 | implements AutoCloseable 21 | { 22 | private static final String CLASSNAME = "%Library.MessageDictionary"; 23 | private static final String METHODNAME = "SetSessionLanguage"; 24 | private static final String LANG = "en-us"; 25 | 26 | private final Database database; 27 | 28 | /** 29 | * Constructor 30 | * 31 | * @param jdbcUrl the JDBC URL (jdbc://Cache/etcetc) 32 | * @param user the user 33 | * @param password the password 34 | * @throws CacheException failure to open the database 35 | */ 36 | public CacheDb(final String jdbcUrl, final String user, 37 | final String password) 38 | throws CacheException 39 | { 40 | database = CacheDatabase.getDatabase(jdbcUrl, user, password); 41 | 42 | /* 43 | * Set the session language to English 44 | */ 45 | final Dataholder[] holders = { Dataholder.create(LANG) }; 46 | database.runClassMethod(CLASSNAME, METHODNAME, holders, 47 | Database.RET_NONE); 48 | } 49 | 50 | /** 51 | * Get the underlying Caché {@link Database} 52 | * 53 | * @return the database 54 | */ 55 | public Database getDatabase() 56 | { 57 | return database; 58 | } 59 | 60 | /** 61 | * Get a closeable query from the database 62 | * 63 | * @param provider the function for obtaining the query (example: {@link 64 | * ClassDefinition#query_Summary(Database)} 65 | * @return the query 66 | */ 67 | public CacheSqlQuery query(final CacheQueryProvider provider) 68 | throws CacheException 69 | { 70 | final CacheQuery query = provider.getQuery(database); 71 | return new CacheSqlQuery(query); 72 | } 73 | 74 | @Override 75 | public void close() 76 | throws CacheException 77 | { 78 | final int count = database.close().size(); 79 | 80 | if (count > 0) 81 | System.err.printf("%d objects not freed when closing database", 82 | count); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/commands/ExportCommand.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.commands; 2 | 3 | import com.intersys.objects.CacheException; 4 | import es.litesolutions.cache.db.CacheDb; 5 | import es.litesolutions.cache.Util; 6 | 7 | import java.io.IOException; 8 | import java.nio.file.Files; 9 | import java.nio.file.Path; 10 | import java.nio.file.Paths; 11 | import java.sql.SQLException; 12 | import java.util.Map; 13 | import java.util.Set; 14 | import java.util.regex.Pattern; 15 | 16 | /** 17 | * Export command: write the source of all classes to a given directory 18 | */ 19 | public final class ExportCommand 20 | extends CachedbCommand 21 | { 22 | private static final Pattern DOT = Pattern.compile("\\."); 23 | 24 | private static final String OUTPUTDIR = "outputDir"; 25 | 26 | private static final String OVERWRITE = "overwrite"; 27 | private static final String OVERWRITE_DEFAULT = "false"; 28 | 29 | private final Path outputDir; 30 | private final boolean overwrite; 31 | 32 | public ExportCommand(final CacheDb cacheDb, 33 | final Map arguments) 34 | { 35 | super(cacheDb, arguments); 36 | outputDir = Paths.get(getArgument(OUTPUTDIR)).toAbsolutePath(); 37 | overwrite = Boolean.parseBoolean(getArgumentOrDefault(OVERWRITE, 38 | OVERWRITE_DEFAULT)); 39 | } 40 | 41 | @Override 42 | public void execute() 43 | throws CacheException, SQLException, IOException 44 | { 45 | prepareDirectory(); 46 | 47 | final Set classes = runner.listClasses(includeSys); 48 | 49 | writeClasses(classes); 50 | } 51 | 52 | void prepareDirectory() 53 | throws IOException 54 | { 55 | if (Files.exists(outputDir)) { 56 | if (!overwrite) { 57 | System.err.printf("directory %s already exists", outputDir); 58 | System.exit(2); 59 | } 60 | Util.deleteDirectory(outputDir); 61 | } 62 | 63 | Files.createDirectories(outputDir); 64 | } 65 | 66 | void writeClasses(final Set classes) 67 | throws CacheException, IOException 68 | { 69 | Path out; 70 | 71 | for (final String className: classes) { 72 | out = computePath(className); 73 | Files.createDirectories(out.getParent()); 74 | runner.writeClassContent(className, out); 75 | } 76 | } 77 | 78 | private Path computePath(final String className) 79 | { 80 | final String[] parts = DOT.split(className); 81 | final int len = parts.length; 82 | final String lastPart = parts[len - 1]; 83 | 84 | Path ret = outputDir; 85 | 86 | for (int i = 0; i < len - 1; i++) 87 | ret = ret.resolve(parts[i]); 88 | 89 | return ret.resolve(lastPart + ".cls"); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What this is 2 | 3 | This is a command line program which allows to perform the following: 4 | 5 | * list classes from a given namespace; 6 | * import an XML file into a namespace; 7 | * export classes in a namespace as source files; 8 | * combine both import and export: take an XML export, import it and generate the 9 | source files. 10 | 11 | This program **needs not** run on the same machine as the Caché installation. 12 | 13 | **Requires Java 8**. License is ASL 2.0. 14 | 15 | ## Status 16 | 17 | The jar basically works; however, because InterSystems jars are not 18 | redistributable (that I know of, at least) and this program needs it, you have 19 | to build it yourself; see the instructions below. 20 | 21 | ## How to use 22 | 23 | * Clone this project. 24 | * Have a JDK 8 install. 25 | * Copy `cachedb.jar` and `cachejdbc.jar` from a Caché installation into the 26 | `lib` directory of the cloned project. 27 | * Build the jar with `./gradlew shadowJar`. 28 | 29 | ## Running 30 | 31 | The generated jar is fully contained, and generated as 32 | `build/libs/cachedb-import.jar`. 33 | 34 | You can view a global help with: 35 | 36 | ``` 37 | java -jar build/libs/cachedb-import.jar help 38 | ``` 39 | 40 | It is recommended that you use a property file with the following keys, so that 41 | the command line is not too long, and reuse it across your commands: 42 | 43 | 44 | ``` 45 | # Host where the Caché installation is. 46 | # If not specified, the default is localhost. 47 | host = some.host.somewhere 48 | # Port to connect to. 49 | # If not specified, the default is 1972. 50 | port = 1972 51 | # REQUIRED: Caché user 52 | user = theuser 53 | # REQUIRED: the password 54 | password = thepassword 55 | # REQUIRED: the namespace 56 | namespace = THENAMESPACE 57 | ``` 58 | 59 | Say this file is named `/tmp/db.properties`; if you have, for instance, an XML 60 | export file named `/tmp/myexport.xml` and want to generate the sources in 61 | directory `/tmp/sources`, you will use this command line: 62 | 63 | ``` 64 | java -jar build/libs/cachedb-import.jar gensrc cfg=/tmp/db.properties 65 | inputFile=/tmp/myexport.xml outputDir=/tmp/sources 66 | ``` 67 | 68 | Please see the help for more options. 69 | 70 | ## Limitations 71 | 72 | ### Stream import (`mode=stream`) has limits 73 | 74 | Unfortunately, the stream import routine provided by Caché is unable to provide 75 | the list of classes which were imported. As a result, when you try and generate 76 | the sources from an XML import, what happens is the following: 77 | 78 | * the list of classes in the namespace is listed before import; 79 | * the XML is imported; 80 | * the list of classes after import is listed; 81 | * the difference between those two lists is then exported. 82 | 83 | ### Imported items are not removed, even if only generating sources 84 | 85 | One such reason is because of the stream import limitation mentioned above. If 86 | you want to delete the imported items, you have to do it by hand. 87 | 88 | ### Only .cls (class definitions) are exported to sources 89 | 90 | However, the import routine _will_ import everything in the XML (globals, 91 | routines, INC files, etc). 92 | 93 | ## Problems 94 | 95 | ### Unable to use the file import with some environments 96 | 97 | This environment is known to completely fail with file based import, for reasons 98 | still not understood: 99 | 100 | * Windows 8.1, x86_64; 101 | * Caché 2015.3. 102 | 103 | The problems with this environment are, along others: 104 | 105 | * if a character stream is used, the result file on the server is corrupted; 106 | * if a binary stream is used, the result file is not corrupted, but the import 107 | fails. 108 | 109 | Which means that for this environment in particular, and maybe others, there is 110 | no choice but to use the streaming import -- at least until the reasons for 111 | these failures are understood and possibly fixed. 112 | 113 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/CacheImport.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache; 2 | 3 | import es.litesolutions.cache.commands.CachedbCommand; 4 | import es.litesolutions.cache.commands.ExportCommand; 5 | import es.litesolutions.cache.commands.GensrcCommand; 6 | import es.litesolutions.cache.commands.ImportCommand; 7 | import es.litesolutions.cache.commands.ListClassesCommand; 8 | import es.litesolutions.cache.db.CacheDb; 9 | 10 | import java.io.Reader; 11 | import java.nio.file.Files; 12 | import java.nio.file.Paths; 13 | import java.util.Collections; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | import java.util.Properties; 17 | 18 | /** 19 | * The entry point 20 | */ 21 | public final class CacheImport 22 | { 23 | private static final String HOST = "host"; 24 | private static final String HOST_DEFAULT = "localhost"; 25 | 26 | private static final String PORT = "port"; 27 | private static final String PORT_DEFAULT = "1972"; 28 | 29 | private static final String USER = "user"; 30 | 31 | private static final String PASSWORD = "password"; 32 | 33 | private static final String NAMESPACE = "namespace"; 34 | 35 | private static final String JDBC_URL_TEMPLATE = "jdbc:Cache://%s:%s/%s"; 36 | 37 | private static final Map COMMANDS; 38 | 39 | static { 40 | final Map map = new HashMap<>(); 41 | 42 | map.put("listClasses", ListClassesCommand::new); 43 | map.put("export", ExportCommand::new); 44 | map.put("import", ImportCommand::new); 45 | map.put("gensrc", GensrcCommand::new); 46 | 47 | COMMANDS = Collections.unmodifiableMap(map); 48 | } 49 | 50 | private CacheImport() 51 | { 52 | throw new Error("instantiation not permitted"); 53 | } 54 | 55 | public static void main(final String... args) 56 | throws Exception 57 | { 58 | if (args.length < 1) { 59 | Util.readHelp(); 60 | System.exit(2); 61 | } 62 | 63 | final String cmdName = args[0]; 64 | final CommandCreator creator = COMMANDS.get(cmdName); 65 | 66 | if ("help".equals(cmdName)) { 67 | Util.readHelp(); 68 | System.exit(2); 69 | } 70 | 71 | if (creator == null) { 72 | System.err.printf("Unknown command '%s'%n", cmdName); 73 | Util.readHelp(); 74 | System.exit(2); 75 | } 76 | 77 | if (args.length >= 2 && "help".equals(args[1])) { 78 | Util.readHelp(cmdName); 79 | System.exit(2); 80 | } 81 | 82 | final Map cfg = getCfg(args); 83 | 84 | final String cfgFile = cfg.get("cfg"); 85 | 86 | if (cfgFile != null) { 87 | final Properties properties = new Properties(); 88 | try ( 89 | final Reader reader = Files.newBufferedReader( 90 | Paths.get(cfgFile)); 91 | ) { 92 | properties.load(reader); 93 | for (final String key: properties.stringPropertyNames()) 94 | cfg.putIfAbsent(key, properties.getProperty(key)); 95 | } 96 | } 97 | 98 | /* 99 | * Basic arguments required to generate the JDBC URL 100 | */ 101 | final String host = Util.readOrDefault(HOST, cfg, HOST_DEFAULT); 102 | final String port = Util.readOrDefault(PORT, cfg, PORT_DEFAULT); 103 | final String namespace = Util.readArgument(NAMESPACE, cfg); 104 | 105 | final String jdbcUrl = String.format(JDBC_URL_TEMPLATE, host, port, 106 | namespace); 107 | 108 | /* 109 | * Now the user and password 110 | */ 111 | final String user = Util.readArgument(USER, cfg); 112 | final String password = Util.readArgument(PASSWORD, cfg); 113 | 114 | try ( 115 | final CacheDb db = new CacheDb(jdbcUrl, user, password); 116 | ) { 117 | creator.create(db, cfg).execute(); 118 | } 119 | } 120 | 121 | private static Map getCfg(final String[] args) 122 | { 123 | final Map ret = new HashMap<>(); 124 | 125 | String name; 126 | String value; 127 | int index; 128 | 129 | for (final String arg: args) { 130 | index = arg.indexOf('='); 131 | if (index == -1) 132 | continue; 133 | name = arg.substring(0, index); 134 | value = arg.substring(index + 1, arg.length()); 135 | ret.put(name, value); 136 | } 137 | 138 | return ret; 139 | } 140 | 141 | @FunctionalInterface 142 | private interface CommandCreator 143 | { 144 | CachedbCommand create(CacheDb cacheDb, Map cfg); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/Util.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.Reader; 8 | import java.net.URL; 9 | import java.nio.charset.CharsetDecoder; 10 | import java.nio.charset.CodingErrorAction; 11 | import java.nio.charset.StandardCharsets; 12 | import java.nio.file.FileVisitResult; 13 | import java.nio.file.FileVisitor; 14 | import java.nio.file.Files; 15 | import java.nio.file.Path; 16 | import java.nio.file.SimpleFileVisitor; 17 | import java.nio.file.attribute.BasicFileAttributes; 18 | import java.util.Map; 19 | import java.util.Objects; 20 | 21 | /** 22 | * A set of utility methods, as the name says 23 | */ 24 | public final class Util 25 | { 26 | private Util() 27 | { 28 | throw new Error("instantiation not permitted"); 29 | } 30 | 31 | /** 32 | * Delete a directory recursively 33 | * 34 | *

Note that the first failure to delete a file/directory will stop the 35 | * deletion.

36 | * 37 | * @param victim the directory to delete 38 | * @throws IOException failure to delete a file/directory 39 | */ 40 | public static void deleteDirectory(final Path victim) 41 | throws IOException 42 | { 43 | final FileVisitor visitor = new RecursiveDeletion(); 44 | Files.walkFileTree(victim, visitor); 45 | } 46 | 47 | /** 48 | * Read a help text from a given resource path 49 | * 50 | * @param cmdName the command 51 | * @throws IOException read failure 52 | */ 53 | public static void readHelp(final String cmdName) 54 | throws IOException 55 | { 56 | final URL url = CacheImport.class.getResource('/' + cmdName + ".txt"); 57 | 58 | if (url == null) { 59 | System.err.println("What the... Cannot find help text :("); 60 | System.exit(-1); 61 | } 62 | 63 | final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder() 64 | .onMalformedInput(CodingErrorAction.REPORT); 65 | 66 | try ( 67 | final InputStream in = url.openStream(); 68 | final Reader reader = new InputStreamReader(in, decoder); 69 | final BufferedReader br = new BufferedReader(reader); 70 | ) { 71 | String line; 72 | 73 | while ((line = br.readLine()) != null) 74 | System.err.println(line); 75 | } 76 | } 77 | 78 | /** 79 | * Read the main help text 80 | * 81 | * @throws IOException failure to read 82 | */ 83 | public static void readHelp() 84 | throws IOException 85 | { 86 | readHelp("help"); 87 | } 88 | 89 | /** 90 | * Read a required argument from a map 91 | * 92 | *

This method checks that both arguments are not null. The result is 93 | * guaranteed not to be null as well.

94 | * 95 | * @param key the key in the map 96 | * @param map the map 97 | * @return the value 98 | * @throws IllegalArgumentException no such key in the map 99 | */ 100 | public static String readArgument(final String key, 101 | final Map map) 102 | { 103 | Objects.requireNonNull(key); 104 | Objects.requireNonNull(map); 105 | final String value = map.get(key); 106 | if (value == null) 107 | throw new IllegalArgumentException("required argument '" + key 108 | + "' is missing"); 109 | return value; 110 | } 111 | 112 | /** 113 | * Read an optional argument from the map, returning a default if the key 114 | * is not found 115 | * 116 | *

All arguments must not be null. The result is also guaranteed not to 117 | * be null.

118 | * 119 | * @param key the key 120 | * @param map the map 121 | * @param defaultValue the value to return if the key is not found 122 | * @return the value 123 | */ 124 | public static String readOrDefault(final String key, 125 | final Map map, final String defaultValue) 126 | { 127 | Objects.requireNonNull(key); 128 | Objects.requireNonNull(map); 129 | Objects.requireNonNull(defaultValue); 130 | return map.getOrDefault(key, defaultValue); 131 | } 132 | 133 | private static final class RecursiveDeletion 134 | extends SimpleFileVisitor 135 | { 136 | @Override 137 | public FileVisitResult visitFile(final Path file, 138 | final BasicFileAttributes attrs) 139 | throws IOException 140 | { 141 | Files.delete(file); 142 | return FileVisitResult.CONTINUE; 143 | } 144 | 145 | @Override 146 | public FileVisitResult visitFileFailed(final Path file, 147 | final IOException exc) 148 | throws IOException 149 | { 150 | throw exc; 151 | } 152 | 153 | @Override 154 | public FileVisitResult postVisitDirectory(final Path dir, 155 | final IOException exc) 156 | throws IOException 157 | { 158 | Files.delete(dir); 159 | return FileVisitResult.CONTINUE; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/commands/ImportCommand.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache.commands; 2 | 3 | import com.intersys.objects.CacheException; 4 | import es.litesolutions.cache.db.CacheDb; 5 | 6 | import java.io.IOException; 7 | import java.io.UncheckedIOException; 8 | import java.nio.file.Files; 9 | import java.nio.file.Path; 10 | import java.nio.file.Paths; 11 | import java.nio.file.attribute.BasicFileAttributes; 12 | import java.sql.SQLException; 13 | import java.util.Collections; 14 | import java.util.HashSet; 15 | import java.util.List; 16 | import java.util.Map; 17 | import java.util.Objects; 18 | import java.util.Set; 19 | import java.util.function.BiPredicate; 20 | import java.util.stream.Collectors; 21 | import java.util.stream.Stream; 22 | 23 | /** 24 | * Import command: import an XML into the database 25 | */ 26 | public final class ImportCommand 27 | extends CachedbCommand 28 | { 29 | 30 | private static final String MODE = "mode"; 31 | private static final String FILE_MODE = "file"; 32 | private static final String STREAM_MODE = "stream"; 33 | 34 | private static final String INPUT_FILE = "inputFile"; 35 | private static final String INPUT_DIR = "inputDir"; 36 | 37 | private final List files; 38 | private final String mode; 39 | 40 | public ImportCommand(final CacheDb cacheDb, 41 | final Map arguments) 42 | { 43 | super(cacheDb, arguments); 44 | 45 | final String inputFile = arguments.get(INPUT_FILE); 46 | final String inputDir = arguments.get(INPUT_DIR); 47 | 48 | files = collectPaths(inputFile, inputDir); 49 | 50 | mode = getArgumentOrDefault(MODE, FILE_MODE); 51 | 52 | switch (mode) { 53 | case FILE_MODE: 54 | case STREAM_MODE: 55 | return; 56 | default: 57 | System.err.println("Unknown import mode " + mode); 58 | System.exit(2); 59 | } 60 | } 61 | 62 | @Override 63 | public void execute() 64 | throws CacheException, IOException 65 | { 66 | switch (mode) { 67 | case FILE_MODE: 68 | importFile(); 69 | break; 70 | case STREAM_MODE: 71 | importStream(); 72 | break; 73 | default: 74 | throw new Error("Unreachable; how did I get there?"); 75 | } 76 | } 77 | 78 | Set importAndList() 79 | throws CacheException, IOException, SQLException 80 | { 81 | switch (mode) { 82 | case FILE_MODE: 83 | return importFile(); 84 | case STREAM_MODE: 85 | return importStreamAndList(); 86 | default: 87 | throw new Error("Unreachable; how did I get there?"); 88 | } 89 | } 90 | 91 | private Set importFile() 92 | throws CacheException, IOException 93 | { 94 | final Set set = new HashSet<>(); 95 | 96 | for (final Path path: files) 97 | set.addAll(runner.importFile(path, includeSys)); 98 | 99 | return Collections.unmodifiableSet(set); 100 | } 101 | 102 | private Set importStreamAndList() 103 | throws CacheException, SQLException, IOException 104 | { 105 | final Set before = runner.listClasses(includeSys); 106 | importStream(); 107 | final Set after = new HashSet<>(runner.listClasses(includeSys)); 108 | after.removeAll(before); 109 | return Collections.unmodifiableSet(after); 110 | } 111 | 112 | private void importStream() 113 | throws CacheException, IOException 114 | { 115 | for (final Path path: files) 116 | runner.importStream(path); 117 | } 118 | 119 | private static List collectPaths(final String inputFile, 120 | final String inputDir) 121 | { 122 | if (Stream.of(inputDir, inputFile).allMatch(Objects::isNull)) { 123 | System.err.println("Missing required argument (either inputFile" 124 | + " or inputDir must be specified)"); 125 | System.exit(2); 126 | throw new Error("Unrechable! How did I get there?"); 127 | } 128 | 129 | if (Stream.of(inputDir, inputFile).allMatch(Objects::nonNull)) { 130 | System.err.println("Only one of inputDir or inputFile can be" 131 | + " specified"); 132 | System.exit(2); 133 | throw new Error("Unrechable! How did I get there?"); 134 | } 135 | 136 | if (inputFile != null) 137 | return Collections.singletonList(Paths.get(inputFile)); 138 | 139 | if (inputDir != null) 140 | try { 141 | return collectFiles(inputDir); 142 | } catch (IOException e) { 143 | throw new UncheckedIOException(e); 144 | } 145 | 146 | throw new Error("Unreachable! How did I get there?"); 147 | } 148 | 149 | private static List collectFiles(final String inputDir) 150 | throws IOException 151 | { 152 | final Path dir = Paths.get(inputDir).toRealPath(); 153 | final BiPredicate predicate 154 | = (path, attrs) -> attrs.isRegularFile() && path.getFileName() 155 | .toString().toLowerCase().endsWith(".xml"); 156 | try ( 157 | final Stream stream = Files.find(dir, Integer.MAX_VALUE, 158 | predicate); 159 | ) { 160 | return stream.collect(Collectors.toList()); 161 | } 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/main/java/es/litesolutions/cache/CacheRunner.java: -------------------------------------------------------------------------------- 1 | package es.litesolutions.cache; 2 | 3 | import com.intersys.cache.Dataholder; 4 | import com.intersys.classes.CharacterStream; 5 | import com.intersys.classes.Dictionary.ClassDefinition; 6 | import com.intersys.classes.FileBinaryStream; 7 | import com.intersys.classes.GlobalCharacterStream; 8 | import com.intersys.objects.CacheException; 9 | import com.intersys.objects.Database; 10 | import com.intersys.objects.StringHolder; 11 | import es.litesolutions.cache.db.CacheDb; 12 | import es.litesolutions.cache.db.CacheQueryProvider; 13 | import es.litesolutions.cache.db.CacheSqlQuery; 14 | 15 | import java.io.IOException; 16 | import java.io.Reader; 17 | import java.io.Writer; 18 | import java.nio.file.Files; 19 | import java.nio.file.Path; 20 | import java.sql.ResultSet; 21 | import java.sql.SQLException; 22 | import java.util.Collections; 23 | import java.util.HashSet; 24 | import java.util.Set; 25 | import java.util.function.Predicate; 26 | import java.util.regex.Pattern; 27 | import java.util.stream.Collectors; 28 | 29 | /** 30 | * A wrapper class for the necessary methods run by this program 31 | * 32 | *

This program takes a {@link CacheDb} as an argument (instead of a plain 33 | * {@link Database}) since the former's {@link CacheDb#query(CacheQueryProvider) 34 | * query method} is needed.

35 | */ 36 | public final class CacheRunner 37 | { 38 | private static final String CRLF = "\r\n"; 39 | 40 | private static final Predicate CLSFILES = s -> s.endsWith(".cls"); 41 | private static final Predicate SYSEXCLUDE = s -> s.charAt(0) != '%'; 42 | 43 | private static final Pattern COMMA = Pattern.compile(","); 44 | 45 | private static final String CLASSDEFINITION_NAME_SQLFIELD = "Name"; 46 | 47 | private static final String LOADSTREAM_CLASSNAME = "%SYSTEM.OBJ"; 48 | private static final String LOADSTREAM_METHODNAME = "LoadStream"; 49 | 50 | private static final String LOADFILE_CLASSNAME = "%SYSTEM.OBJ"; 51 | private static final String LOADFILE_METHODNAME = "Load"; 52 | 53 | private static final String WRITECLASSCONTENT_CLASSNAME 54 | = "%Compiler.UDL.TextServices"; 55 | private static final String WRITECLASSCONTENT_METHODNAME 56 | = "GetTextAsString"; 57 | 58 | private static final String FILE_CLASSNAME = "%File"; 59 | private static final String FILE_METHODNAME = "TempFilename"; 60 | 61 | private final CacheDb cacheDb; 62 | 63 | public CacheRunner(final CacheDb cacheDb) 64 | { 65 | this.cacheDb = cacheDb; 66 | } 67 | 68 | /** 69 | * List the classes for this database 70 | * 71 | * @param includeSys also list system classes 72 | * @return the set of classes 73 | * @throws CacheException Caché error 74 | * @throws SQLException SQL error 75 | */ 76 | @SuppressWarnings("OverlyBroadThrowsClause") 77 | public Set listClasses(final boolean includeSys) 78 | throws CacheException, SQLException 79 | { 80 | final Set set = new HashSet<>(); 81 | 82 | try ( 83 | final CacheSqlQuery query 84 | = cacheDb.query(ClassDefinition::query_Summary); 85 | final ResultSet rs = query.execute(); 86 | ) { 87 | while (rs.next()) { 88 | final String name = rs.getString(CLASSDEFINITION_NAME_SQLFIELD); 89 | /* 90 | * FIXME: meh, that would be better done at the query level 91 | */ 92 | if (includeSys || name.charAt(0) != '%') 93 | set.add(name); 94 | } 95 | } 96 | 97 | return Collections.unmodifiableSet(set); 98 | } 99 | 100 | /** 101 | * Import an XML export as a stream 102 | * 103 | *

This is needed in some situations where the file import does not work. 104 | * Unfortunately, with this mode, you cannot reliably get a list of loaded 105 | * classes. See here for more 107 | * details.

108 | * 109 | *

Prefer to use {@link #importFile(Path, boolean)} instead.

110 | * 111 | * @param path path to the XML export 112 | * @throws CacheException Caché error 113 | * @throws IOException Failed to read from the XML export 114 | */ 115 | public void importStream(final Path path) 116 | throws CacheException, IOException 117 | { 118 | final Database db = cacheDb.getDatabase(); 119 | 120 | final CharacterStream stream = new GlobalCharacterStream(db); 121 | loadContent(stream, path); 122 | 123 | /* 124 | * Arguments for class "%SYSTEM.OBJ", class method "LoadStream" 125 | */ 126 | final Dataholder[] arguments = new Dataholder[8]; 127 | 128 | /* 129 | * Arguments ByRef 130 | * 131 | * Indices start at 1, not 0 132 | * 133 | * FIXME: in fact, they are unused... 134 | */ 135 | final int[] byRefArgs = new int[2]; 136 | 137 | // Arg 3: error log 138 | final StringHolder errorlog = new StringHolder(""); 139 | byRefArgs[0] = 3; 140 | 141 | // Arg 4: list of loaded items 142 | final StringHolder loadedlist = new StringHolder(""); 143 | byRefArgs[1] = 4; 144 | 145 | /* 146 | * Fill arguments 147 | */ 148 | // arg 1: stream 149 | arguments[0] = Dataholder.create(stream); 150 | // arg 2: qspec; we want to ensure that compile works, at least 151 | arguments[1] = new Dataholder("d"); 152 | // arg 3: errorlog 153 | arguments[2] = Dataholder.create(errorlog.value); 154 | // arg 4: loadedlist 155 | arguments[3] = Dataholder.create(loadedlist.value); 156 | // arg 5: listonly; no 157 | arguments[4] = Dataholder.create(Boolean.FALSE); 158 | // arg 6: selecteditems; nothing 159 | arguments[5] = Dataholder.create(null); 160 | // arg 7: displayname. For logging... 161 | arguments[6] = Dataholder.create("IMPORT"); 162 | // arg 8: charset. Default is empty string, we'll assume UTF-8. 163 | arguments[7] = new Dataholder((String) null); 164 | 165 | // Now, make the call 166 | final Dataholder[] result = db.runClassMethod( 167 | LOADSTREAM_CLASSNAME, 168 | LOADSTREAM_METHODNAME, 169 | byRefArgs, 170 | arguments, 171 | Database.RET_PRIM 172 | ); 173 | 174 | /* 175 | * The result normally has three members: 176 | * 177 | * - first is the status; and we need to do that: 178 | */ 179 | db.parseStatus(result[0]); 180 | 181 | /* 182 | * - others are ByRef arguments 183 | */ 184 | // FIXME: not filled correctly... No idea if this is a bug with 185 | // InterSystems jars or not. See the README for more details. 186 | // errorlog.set(result[1].getString()); 187 | // System.out.println("errorlog: " + errorlog.getValue()); 188 | // 189 | // loadedlist.set(result[2].getString()); 190 | // System.out.println("loadedlist: " + loadedlist.getValue()); 191 | } 192 | 193 | /** 194 | * Import an XML as a file 195 | * 196 | *

In fact, this creates a file using the Caché API on the remote server, 197 | * copies the content of the XML to this file and then imports it. Unlike 198 | * what happens with {@link #importStream(Path)}, with this method, you 199 | * do get the list of loaded items back.

200 | * 201 | * @param path path to the XML to import 202 | * @param includeSys include system classes 203 | * @return the list of loaded classes 204 | * @throws CacheException Caché error 205 | * @throws IOException Failure to read from the XML export 206 | */ 207 | public Set importFile(final Path path, final boolean includeSys) 208 | throws CacheException, IOException 209 | { 210 | final String tempFileName = createRemoteTemporaryFileName(); 211 | 212 | final Database db = cacheDb.getDatabase(); 213 | 214 | final FileBinaryStream stream = new FileBinaryStream(db); 215 | stream._filenameSet(tempFileName); 216 | 217 | Files.copy(path, stream.getOutputStream()); 218 | 219 | final String remoteFileName = stream._filenameGet(); 220 | 221 | /* 222 | * Arguments for class "%SYSTEM.OBJ", class method "Load" 223 | */ 224 | final Dataholder[] arguments = new Dataholder[9]; 225 | 226 | /* 227 | * Arguments ByRef 228 | * 229 | * Indices start at 1, not 0 230 | */ 231 | final int[] byRefArgs = new int[3]; 232 | 233 | // Arg 3: error log 234 | final StringHolder errorlog = new StringHolder(""); 235 | byRefArgs[0] = 3; 236 | 237 | // Arg 4: list of loaded items 238 | final StringHolder loadedlist = new StringHolder(""); 239 | byRefArgs[1] = 4; 240 | 241 | // Arg 9: description (?) 242 | final StringHolder description = new StringHolder(""); 243 | byRefArgs[2] = 9; 244 | 245 | /* 246 | * Fill arguments 247 | */ 248 | // arg 1: file name 249 | arguments[0] = Dataholder.create(remoteFileName); 250 | // arg 2: qspec; we want to ensure that compile works, at least 251 | arguments[1] = new Dataholder("d"); 252 | // arg 3: errorlog 253 | arguments[2] = Dataholder.create(errorlog.value); 254 | // arg 4: loadedlist 255 | arguments[3] = Dataholder.create(loadedlist.value); 256 | // arg 5: listonly; no 257 | arguments[4] = Dataholder.create(Boolean.FALSE); 258 | // arg 6: selecteditems; nothing 259 | arguments[5] = Dataholder.create(null); 260 | // arg 7: displayname. For logging... 261 | arguments[6] = Dataholder.create(null); 262 | // arg 8: charset. We force UTF-8. 263 | arguments[7] = new Dataholder("UTF8"); 264 | // arg 9: description (?) 265 | arguments[8] = Dataholder.create(description.value); 266 | 267 | // Now, make the call 268 | final Dataholder[] result = db.runClassMethod( 269 | LOADFILE_CLASSNAME, 270 | LOADFILE_METHODNAME, 271 | byRefArgs, 272 | arguments, 273 | Database.RET_PRIM 274 | ); 275 | 276 | /* 277 | * The result normally has three members: 278 | * 279 | * - first is the status; and we need to do that: 280 | */ 281 | db.parseStatus(result[0]); 282 | 283 | /* 284 | * - others are ByRef arguments 285 | */ 286 | // TODO 287 | // errorlog.set(result[1].getString()); 288 | // System.out.println("errorlog: " + errorlog.getValue()); 289 | 290 | loadedlist.set(result[2].getString()); 291 | 292 | Predicate predicate = CLSFILES; 293 | if (!includeSys) 294 | predicate = predicate.and(SYSEXCLUDE); 295 | 296 | final String value = (String) loadedlist.getValue(); 297 | /* 298 | * It can happen if we have nothing imported... 299 | */ 300 | if (value == null) 301 | return Collections.emptySet(); 302 | final Set set = COMMA.splitAsStream(value) 303 | .filter(predicate) 304 | .map(s -> s.substring(0, s.length() - 4)) 305 | .collect(Collectors.toCollection(HashSet::new)); 306 | return Collections.unmodifiableSet(set); 307 | } 308 | 309 | /** 310 | * Write the source code of a Caché class to a file 311 | * 312 | *

The file is written using UTF-8 and {@code \r\n} as a line terminator. 313 | *

314 | * 315 | * @param className the class name 316 | * @param path path of the file to write 317 | * @throws CacheException Caché error 318 | * @throws IOException Write failure 319 | */ 320 | public void writeClassContent(final String className, final Path path) 321 | throws CacheException, IOException 322 | { 323 | final Database db = cacheDb.getDatabase(); 324 | 325 | final int[] byRefs = new int[1]; 326 | byRefs[0] = 3; 327 | final StringHolder holder = new StringHolder(""); 328 | 329 | final Dataholder[] arguments = new Dataholder[4]; 330 | arguments[0] = new Dataholder((String) null); 331 | arguments[1] = new Dataholder(className); 332 | arguments[2] = Dataholder.create(holder.value); 333 | arguments[3] = new Dataholder(CRLF); 334 | 335 | final Dataholder[] res = db.runClassMethod( 336 | WRITECLASSCONTENT_CLASSNAME, 337 | WRITECLASSCONTENT_METHODNAME, 338 | byRefs, 339 | arguments, 340 | Database.RET_PRIM 341 | ); 342 | 343 | db.parseStatus(res[0]); 344 | 345 | holder.set(res[1].getString()); 346 | 347 | try ( 348 | final Writer writer = Files.newBufferedWriter(path); 349 | ) { 350 | writer.write(holder.value); 351 | } 352 | } 353 | 354 | private String createRemoteTemporaryFileName() 355 | throws CacheException 356 | { 357 | final Dataholder[] args = { new Dataholder("xml") }; 358 | final Dataholder res = cacheDb.getDatabase() 359 | .runClassMethod(FILE_CLASSNAME, FILE_METHODNAME, args, 0); 360 | return res.getString(); 361 | } 362 | 363 | private static void loadContent(final CharacterStream stream, 364 | final Path path) 365 | throws IOException, CacheException 366 | { 367 | final StringBuilder sb = new StringBuilder(); 368 | 369 | try ( 370 | final Reader reader = Files.newBufferedReader(path); 371 | ) { 372 | final char[] buf = new char[2048]; 373 | int nrChars; 374 | 375 | while ((nrChars = reader.read(buf)) != -1) 376 | sb.append(buf, 0, nrChars); 377 | } 378 | 379 | // Note that we don't _rewind() the stream; the loading function does 380 | // that by itself 381 | stream._write(sb.toString()); 382 | } 383 | } 384 | --------------------------------------------------------------------------------