Browse files

Added an example that stores regular content in a local H2 database v…

…ia JDBC
  • Loading branch information...
1 parent ebf9fc3 commit 59648dceece54183cb921a2b299f8bf52dae7c38 @rhauch rhauch committed Feb 18, 2013
View
61 modeshape-jdbc-store-example/README.md
@@ -0,0 +1,61 @@
+# ModeShape Embedded Example
+
+This is a self-contained Maven project that shows how to embed ModeShape
+within a simple J2SE application. The
+[ModeShapeExample](modeshape-embedded-example/src/main/java/org/modeshape/example/embedded/ModeShapeExample.java) class has a 'main(...)' method that loads a ModeShape configuration
+as a resource on the classpath, uses that configuration to build a
+ModeShapeEngine instance, starts the engine, and obtains a JCR Session
+to the repository.
+
+This project is self-contained and can be built at the top level of your
+local clone of the Git repository, or by simply building this project
+using Maven 3:
+
+ $ mvn clean install
+
+See [this ModeShape community article](http://community.jboss.org/wiki/ModeShapeandMaven)
+for help on how to install Maven 3.
+
+# The ModeShape project
+
+MThis project is self-contained and can be built at the top level of your
+local clone of the Git repository, or by simply building this project
+using Maven 3:
+
+ $ mvn clean install
+
+See [this ModeShape community article](http://community.jboss.org/wiki/ModeShapeandMaven)
+for help on how to install Maven 3.
+
+# The ModeShape project
+
+ModeShape is an open source implementation of the JCR 2.0
+([JSR-283](http://www.jcp.org/en/jsr/detail?id=283])) specification and
+standard API. To your applications, ModeShape looks and behaves like a
+regular JCR repository. Applications can search, query, navigate, change,
+version, listen for changes, etc. But ModeShape can store that content
+in a variety of back-end stores (including relational databases, Infinispan
+data grids, JBoss Cache, etc.), or it can access and update existing content
+from *other* kinds of systems (including file systems, SVN repositories,
+JDBC database metadata, and other JCR repositories). ModeShape's connector
+architecture means that you can write custom connectors to access any
+kind of system. And ModeShape can even federate multiple back-end systems
+into a single, unified virtual repository.
+
+For more information on ModeShape, including getting started guides,
+reference guides, and downloadable binaries, visit the project's website
+at [http://www.modeshape.org]() or follow us on our [blog](http://modeshape.wordpress.org)
+or on [Twitter](http://twitter.com/modeshape). Or hop into our
+[IRC chat room](http://www.jboss.org/modeshape/chat) and talk our community
+of contributors and users.
+
+The official Git repository for the project is also on GitHub at
+[http://github.com/ModeShape/modeshape]().
+
+# Need help?
+
+ModeShape is open source software with a dedicated community. If you have
+any questions or problems, post a question in our
+[user forum](http://community.jboss.org/en/modeshape) or hop into our
+[IRC chat room](http://www.jboss.org/modeshape/chat) and talk our
+community of contributors and users.
View
173 modeshape-jdbc-store-example/pom.xml
@@ -0,0 +1,173 @@
+<?xml version="1.0"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.modeshape.example</groupId>
+ <artifactId>modeshape-jdbc-store-example</artifactId>
+ <packaging>jar</packaging>
+ <name>Example that uses ModeShape w/ JDBC-based storage</name>
+ <description>A simple example application that creates an embedded ModeShape engine using local H2 JDBC database for storage, and the file system for indexes and binaries.</description>
+ <version>1.0-SNAPSHOT</version>
+ <properties>
+ <!-- Instruct the build to use only UTF-8 encoding for source code -->
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <!--
+ Global dependency version information
+ -->
+ <modeshape.version>3.1.1.Final</modeshape.version>
+ <jdbc.h2.version>1.2.124</jdbc.h2.version>
+ <c3p0.version>0.9.1.2</c3p0.version>
+ <!--
+ Maven plugin versions
+ -->
+ <maven.surefire.report.plugin.version>2.4.3</maven.surefire.report.plugin.version>
+ <maven.surefire.plugin.version>2.7.1</maven.surefire.plugin.version>
+ <maven.assembly.plugin.version>2.2</maven.assembly.plugin.version>
+ <maven.install.plugin.version>2.3.1</maven.install.plugin.version>
+ <maven.jar.plugin.version>2.3.1</maven.jar.plugin.version>
+ <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
+ <maven.source.plugin.version>2.1.2</maven.source.plugin.version>
+ <maven.resources.plugin.version>2.4.3</maven.resources.plugin.version>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- Import the ModeShape BOM for embedded usage. This adds to the "dependenciesManagement" section
+ defaults for all of the modules we might need, but we still have to include in the
+ "dependencies" section the modules we DO need. The benefit is that we don't have to
+ specify the versions of any of those modules.-->
+ <dependency>
+ <groupId>org.modeshape.bom</groupId>
+ <artifactId>modeshape-bom-embedded</artifactId>
+ <version>${modeshape.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.modeshape</groupId>
+ <artifactId>modeshape-jcr-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.modeshape</groupId>
+ <artifactId>modeshape-jcr</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.modeshape</groupId>
+ <artifactId>modeshape-common</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <!--This examples uses Infinispan's JDBC cache store, which requires C3P0 and a JDBC database driver -->
+ <dependency>
+ <groupId>org.infinispan</groupId>
+ <artifactId>infinispan-cachestore-jdbc</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>c3p0</groupId>
+ <artifactId>c3p0</artifactId>
+ <version>${c3p0.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <version>${jdbc.h2.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ </resource>
+ <resource>
+ <directory>src/main/java</directory>
+ <includes>
+ <include>**</include>
+ </includes>
+ <excludes>
+ <exclude>**/*.java</exclude>
+ </excludes>
+ </resource>
+ </resources>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <includes>
+ <include>**/*Test.java</include>
+ </includes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ <plugins>
+ <!-- Specify the compiler options and settings -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${maven.compiler.plugin.version}</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <showDeprecation>false</showDeprecation>
+ <showWarnings>false</showWarnings>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${maven.surefire.plugin.version}</version>
+ <configuration>
+ <includes>
+ <include>**/*TestCase.java</include>
+ <include>**/*Test.java</include>
+ </includes>
+ </configuration>
+ </plugin>
+ <!-- Produce source jars during the 'verify' phase -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ <version>${maven.source.plugin.version}</version>
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <phase>verify</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <!--
+ Build a test-jar for each project, so that src/test/* resources and
+ classes can be used in other projects. Also customize how the jar
+ files are assembled.
+ -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>${maven.jar.plugin.version}</version>
+ <executions>
+ <execution>
+ <id>test-jar</id>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>JBoss-Releases</id>
+ <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
+ </repository>
+ </repositories>
+</project>
View
91 ...e-jdbc-store-example/src/main/java/org/modeshape/example/filesystem/ModeShapeExample.java
@@ -0,0 +1,91 @@
+package org.modeshape.example.filesystem;
+
+import java.io.ByteArrayInputStream;
+import java.net.URL;
+import java.util.Random;
+import javax.jcr.*;
+import org.modeshape.common.collection.Problems;
+import org.modeshape.jcr.ModeShapeEngine;
+import org.modeshape.jcr.RepositoryConfiguration;
+import org.modeshape.jcr.value.BinaryValue;
+
+public class ModeShapeExample {
+
+ public static void main( String[] argv ) {
+
+ Random rnd = new Random();
+
+ // 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");
+
+ Node n = root.addNode("Node" + rnd.nextInt());
+
+ n.setProperty("key", "value");
+ n.setProperty("content", session.getValueFactory().createBinary(new ByteArrayInputStream(new byte[1000])));
+
+ session.save();
+
+ System.out.println("Added one node under root");
+ System.out.println("+ Root childs");
+
+ NodeIterator it = root.getNodes();
+ while (it.hasNext()) {
+ System.out.println("+---> " + it.nextNode().getName());
+ }
+ } catch (Exception 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();
+ }
+ }
+
+ }
+}
View
40 modeshape-jdbc-store-example/src/main/resources/infinispan-configuration.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:infinispan:config:5.1 http://www.infinispan.org/schemas/infinispan-config-5.1.xsd"
+ xmlns="urn:infinispan:config:5.1">
+ <global>
+ </global>
+
+ <default>
+ </default>
+
+ <namedCache name="persisted_repository">
+ <loaders passivation="false" shared="false" preload="false">
+ <loader class="org.infinispan.loaders.jdbc.stringbased.JdbcStringBasedCacheStore"
+ fetchPersistentState="false" ignoreModifications="false"
+ purgeOnStartup="false">
+ <properties>
+ <property name="stringsTableNamePrefix" value="ISPN_STRING_TABLE"/>
+ <property name="idColumnName" value="ID_COLUMN"/>
+ <property name="dataColumnName" value="DATA_COLUMN"/>
+ <property name="timestampColumnName" value="TIMESTAMP_COLUMN"/>
+ <property name="timestampColumnType" value="BIGINT"/>
+ <property name="connectionFactoryClass" value="org.infinispan.loaders.jdbc.connectionfactory.PooledConnectionFactory"/>
+ <property name="connectionUrl" value="jdbc:h2:file:target/content/db;DB_CLOSE_DELAY=-1"/>
+ <property name="userName" value="sa"/>
+ <property name="driverClass" value="org.h2.Driver"/>
+ <property name="idColumnType" value="VARCHAR(255)"/>
+ <property name="dataColumnType" value="BINARY"/>
+ <property name="dropTableOnExit" value="false"/>
+ <property name="createTableOnStart" value="true"/>
+ </properties>
+ </loader>
+ </loaders>
+
+ <transaction
+ transactionManagerLookupClass="org.infinispan.transaction.lookup.DummyTransactionManagerLookup"
+ transactionMode="TRANSACTIONAL" lockingMode="OPTIMISTIC" />
+
+ </namedCache>
+
+</infinispan>
View
11 modeshape-jdbc-store-example/src/main/resources/log4j.properties
@@ -0,0 +1,11 @@
+# Direct log messages to stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.out
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %m%n
+
+# Root logger option
+log4j.rootLogger=INFO, stdout
+
+# Set up the default logging to be INFO level, then override specific units
+log4j.logger.org.modeshape=INFO
View
33 modeshape-jdbc-store-example/src/main/resources/my-repository-config.json
@@ -0,0 +1,33 @@
+{
+ "name" : "Persisted-Repository",
+ "workspaces" : {
+ "predefined" : ["otherWorkspace"],
+ "default" : "default",
+ "allowCreation" : true
+ },
+ "security" : {
+ "anonymous" : {
+ "roles" : ["readonly","readwrite","admin"],
+ "useOnFailedLogin" : false
+ }
+ },
+ "storage" : {
+ "cacheConfiguration" : "infinispan-configuration.xml",
+ "cacheName" : "persisted_repository",
+ "binaryStorage" : {
+ "type" : "file",
+ "directory": "target/content/binaries",
+ "minimumBinarySizeInBytes" : 999
+ }
+ },
+ "query":{
+ "enabled":true,
+ "rebuildUponStartup":"if_missing",
+ "indexStorage": {
+ "type":"filesystem",
+ "location":"target/content/index",
+ "lockingStrategy":"simple",
+ "fileSystemAccessType":"auto"
+ }
+ },
+}
View
37 ...jdbc-store-example/src/test/java/org/modeshape/example/embedded/ModeShapeExampleTest.java
@@ -0,0 +1,37 @@
+/*
+ * ModeShape (http://www.modeshape.org)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * ModeShape is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.modeshape.example.embedded;
+
+import org.modeshape.example.filesystem.ModeShapeExample;
+import org.junit.Test;
+
+public class ModeShapeExampleTest {
+
+ @Test
+ public void shouldRunApplication() throws Exception {
+ String[] argv = new String[]{};
+ ModeShapeExample.main(argv);
+ }
+}
View
11 modeshape-jdbc-store-example/src/test/resources/log4j.properties
@@ -0,0 +1,11 @@
+# Direct log messages to stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.out
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %m%n
+
+# Root logger option
+log4j.rootLogger=INFO, stdout
+
+# Set up the default logging to be INFO level, then override specific units
+log4j.logger.org.modeshape=INFO
View
1 pom.xml
@@ -23,6 +23,7 @@
<module>modeshape-sequencing-example</module>
<module>modeshape-ddl-parser-example</module>
<module>modeshape-filesystem-store-example</module>
+ <module>modeshape-jdbc-store-example</module>
<module>modeshape-web-example</module>
<module>modeshape-jca-example</module>
</modules>

0 comments on commit 59648dc

Please sign in to comment.