package oracle.sample.jcr; import java.io.ByteArrayInputStream; import java.util.Hashtable; import javax.jcr.SimpleCredentials; import javax.naming.Context; import oracle.ocs.jcr.OracleNode; import oracle.ocs.jcr.OracleRepository; import oracle.ocs.jcr.OracleSession; import oracle.ocs.jcr.client.impl.RepositoryFactory; public class JcrClient { public static void main(String[] args) { // Complete path to Documents Folder String docFolderPath = "/oracle/beeadmin's Personal Workspace/Documents"; try { // Step 1 : Repository Initialization // Create RepositoryFactory object RepositoryFactory fac = RepositoryFactory.getInstance(); // Create new Hashtable to pass connection attributes Hashtable env = new Hashtable(); // Put the Initial Context Factory Object in the hashtable, which is used // for resolving the names during Java Naming and Directory Interface (JNDI) // lookup; oracle.j2ee.rmi.RMIInitialContextFactory is used for this. env.put( Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory"); // Set the identity of the security principal to the OC4J principal, // which has permissions to look up the service. By default, the oc4jadmin // user has the permissions to look up the Oracle Beehive JCR service env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin"); env.put(Context.SECURITY_CREDENTIALS, "Welcome1"); // The provider URL has the following form: // // opmn:ormi://::/jcr-service // // is the OPMN request port and is the name // of the OC4J instance where the Oracle Beehive JCR service is deployed. // // Obtain the value of the OPMN request port from the file // /opmn/conf/opmn.xml. Search for "request". // // Obtain the name of the OC4J instance by calling the following command: // // beectl list_properties --component _JcrServletService env.put( Context.PROVIDER_URL, "opmn:ormi://example.com:6003:BEEAPP/jcr-service"); // Get Repository Repository rep = fac.getRepository(env); // Create the javax.jcr.SimpleCredentials object by giving the username and // password of the repository SimpleCredentials cred = new SimpleCredentials("beeadmin", "Welcome1".toCharArray()); // Login with the Repository object using the SimpleCredentials object. // A succesful login returns a Session object OracleSession session = (OracleSession)rep.login(cred); System.out.println("Successfully logged in. Session Object " + session); // Step 2: Add Folder Node OracleNode docFolder = (OracleNode)session.getItem(docFolderPath); String folderName = "NewFolder" + System.currentTimeMillis(); docFolder.addNode( folderName, oracle.ocs.jcr.nodetype.OracleNodeType.BEEHIVE_FOLDER); // Save the session session.save(); System.out.println("Added Folder Node to Workspace Node"); // Step 3: Adding Content and Accessing it // Retrieve a node. This example assumes that the folder specified // in docFolderPath exists. OracleNode myNode = (OracleNode)session.getItem(docFolderPath + "/" + folderName); String fileName = "NewFile" + System.currentTimeMillis(); System.out.println("About to add file " + fileName); // Add a child node of type BEEHIVE_FILE OracleNode newNode = (OracleNode)myNode.addNode( fileName, oracle.ocs.jcr.nodetype.OracleNodeType.BEEHIVE_FILE); // Add a property of type BEEHIVE_CONTENT System.out.println("Setting property of type BEEHIVE_CONTENT"); newNode.setProperty( oracle.ocs.jcr.OracleProperty.BEEHIVE_CONTENT, new ByteArrayInputStream("I got updated".getBytes())); // Persist changes by calling session.save() session.save(); System.out.println("Added the file and updated the content"); // Access the file Node by giving the complete path OracleNode docNode = (OracleNode)session.getItem( docFolderPath + "/" + folderName + "/" + fileName); // Access the file content and print it. String content = docNode.getProperty( oracle.ocs.jcr.OracleProperty.BEEHIVE_CONTENT ).getString(); System.out.println("Content = " + content); // Step 4: Removing the Node // Remove the file node docNode.remove(); // Persist the changes session.save(); System.out.println("Removed node"); } catch (Exception ex) { System.out.println("The exception Message:" + ex.getMessage()); ex.printStackTrace(); } } }