public static void main(String[] argv) { // Create and start the engine ... ModeShapeEngine engine = new ModeShapeEngine(); engine.start(); // Load the configuration for a repository via the classloader (can also use path to a file)... Repository repository = null; String repositoryName = null; try { URL url = ModeShapeExample.class.getClassLoader().getResource("my-repository-config.json"); RepositoryConfiguration config = RepositoryConfiguration.read(url); // We could change the name of the repository programmatically ... // config = config.withName("Some Other Repository"); // Verify the configuration for the repository ... Problems problems = config.validate(); if (problems.hasErrors()) { System.err.println("Problems starting the engine."); System.err.println(problems); System.exit(-1); } // Deploy the repository ... repository = engine.deploy(config); repositoryName = config.getName(); } catch (Throwable e) { e.printStackTrace(); System.exit(-1); return; } Session session = null; try { // Get the repository repository = engine.getRepository(repositoryName); // Create a session ... session = repository.login("default"); // Get the root node ... Node root = session.getRootNode(); assert root != null; System.out.println( "Found the root node in the \"" + session.getWorkspace().getName() + "\" workspace"); } catch (RepositoryException e) { e.printStackTrace(); } finally { if (session != null) session.logout(); System.out.println("Shutting down engine ..."); try { engine.shutdown().get(); System.out.println("Success!"); } catch (Exception e) { e.printStackTrace(); } } }
@Override public void start(StartContext arg0) throws StartException { ModeShapeEngine engine = getEngine(); try { final String repositoryName = repositoryName(); // Get the index storage configuration ... IndexStorage indexStorageConfig = indexStorageConfigInjector.getValue(); Document queryConfig = null; if (indexStorageConfig != null) { queryConfig = indexStorageConfig.getQueryConfiguration(); } else { // We'll use the default index storage, but this will be overwritten by the *IndexStorageAdd // operation // (that we're dependent upon). The default for non-AS7 ModeShape repositories is to use // RAM index storage, but in AS7 we want to by default store the indexes on the filesystem // in the // AS7 data directory. // We'll do this by setting a path relative to the data directory, and then injecting // the "${jboss.server.data.dir}/modeshape" path into the repository service // (which will then update the configuration prior to deployment) ... EditableDocument query = Schematic.newDocument(); EditableDocument indexing = query.getOrCreateDocument(FieldName.INDEXING); EditableDocument indexStorage = query.getOrCreateDocument(FieldName.INDEX_STORAGE); EditableDocument backend = indexing.getOrCreateDocument(FieldName.INDEXING_BACKEND); query.set(FieldName.REBUILD_UPON_STARTUP, QueryRebuild.IF_MISSING.toString().toLowerCase()); backend.set(FieldName.TYPE, FieldValue.INDEXING_BACKEND_TYPE_LUCENE); indexStorage.set(FieldName.TYPE, FieldValue.INDEX_STORAGE_FILESYSTEM); String dataDirPath = dataDirectoryPathInjector.getValue(); indexStorage.set( FieldName.INDEX_STORAGE_LOCATION, dataDirPath + "/" + repositoryName + "/indexes"); queryConfig = query; } assert queryConfig != null; // Get the binary storage configuration ... Document binaryConfig = null; BinaryStorage binaryStorageConfig = binaryStorageInjector.getValue(); if (binaryStorageConfig != null) { binaryConfig = binaryStorageConfig.getBinaryConfiguration(); } else { // By default, store the binaries in the data directory ... EditableDocument binaries = Schematic.newDocument(); binaries.set(FieldName.TYPE, FieldValue.BINARY_STORAGE_TYPE_FILE); String dataDirPath = dataDirectoryPathInjector.getValue(); binaries.set(FieldName.DIRECTORY, dataDirPath + "/" + repositoryName + "/binaries"); binaryConfig = binaries; } // Create a new configuration document ... EditableDocument config = Schematic.newDocument(repositoryConfiguration.getDocument()); config.setDocument(FieldName.QUERY, queryConfig); config .getOrCreateDocument(FieldName.STORAGE) .setDocument(FieldName.BINARY_STORAGE, binaryConfig); if (LOG.isDebugEnabled()) { LOG.debugv("ModeShape configuration for '{0}' repository: {1}", repositoryName, config); Problems problems = repositoryConfiguration.validate(); if (problems.isEmpty()) { LOG.debugv( "Problems with configuration for '{0}' repository: {1}", repositoryName, problems); } } // Create a new (updated) configuration ... repositoryConfiguration = new RepositoryConfiguration(config, repositoryName); // Deploy the repository and use this as the environment ... engine.deploy(repositoryConfiguration.with(this)); } catch (ConfigurationException e) { throw new StartException(e); } catch (RepositoryException e) { throw new StartException(e); } }