chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject svn commit: r1516624 - in /chemistry/opencmis/trunk/chemistry-opencmis-client: chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/ chemistry-opencmis-client-api/src/test/ chemistry-opencmis-client-api/src/test/java/ chemis...
Date Thu, 22 Aug 2013 21:39:59 GMT
Author: fmui
Date: Thu Aug 22 21:39:58 2013
New Revision: 1516624

URL: http://svn.apache.org/r1516624
Log:
added SessionFactoryFinder to client API module

Added:
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/SessionFactoryFinder.java
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/client/
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/client/SessionFactoryFinderTest.java
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/SessionFactoryFinderTest.java

Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/SessionFactoryFinder.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/SessionFactoryFinder.java?rev=1516624&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/SessionFactoryFinder.java
(added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/SessionFactoryFinder.java
Thu Aug 22 21:39:58 2013
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+
+import org.apache.chemistry.opencmis.client.api.SessionFactory;
+
+/**
+ * Finds a {@link SessionFactory} implementation and creates an object.
+ */
+public class SessionFactoryFinder {
+
+    /**
+     * Creates a default {@link SessionFactory} object.
+     */
+    public static SessionFactory find() throws ClassNotFoundException, InstantiationException
{
+        return find("org.apache.chemistry.opencmis.client.SessionFactory", null);
+    }
+
+    /**
+     * Creates a {@link SessionFactory} object.
+     * 
+     * @param factoryId
+     *            the factory id of the {@link SessionFactory}
+     */
+    public static SessionFactory find(String factoryId) throws ClassNotFoundException, InstantiationException
{
+        return find(factoryId, null);
+    }
+
+    /**
+     * Creates a {@link SessionFactory} object.
+     * 
+     * @param factoryId
+     *            the factory id of the {@link SessionFactory}
+     * @param classLoader
+     *            the class loader to use
+     */
+    public static SessionFactory find(String factoryId, ClassLoader classLoader) throws ClassNotFoundException,
+            InstantiationException {
+        return find(factoryId, classLoader, "org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl");
+    }
+
+    private static SessionFactory find(String factoryId, ClassLoader classLoader, String
fallbackClassName)
+            throws ClassNotFoundException, InstantiationException {
+        ClassLoader cl = classLoader;
+        if (cl == null) {
+            cl = Thread.currentThread().getContextClassLoader();
+            if (cl == null) {
+                cl = SessionFactoryFinder.class.getClassLoader();
+            }
+        }
+
+        String factoryClassName = null;
+
+        if (factoryId != null) {
+            factoryClassName = System.getProperty(factoryId);
+
+            if (factoryClassName == null) {
+                String serviceId = "META-INF/services/" + factoryId;
+                InputStream stream = cl.getResourceAsStream(serviceId);
+                if (stream != null) {
+                    BufferedReader reader = null;
+                    try {
+                        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
+                        factoryClassName = reader.readLine();
+                        reader.close();
+                    } catch (Exception e) {
+                        factoryClassName = null;
+                    }
+                }
+            }
+        }
+
+        if (factoryClassName == null) {
+            factoryClassName = fallbackClassName;
+        }
+
+        Class<?> clazz = cl.loadClass(factoryClassName);
+
+        SessionFactory result = null;
+        try {
+            Method newInstanceMethod = clazz.getMethod("newInstance", new Class[0]);
+
+            if (!SessionFactory.class.isAssignableFrom(newInstanceMethod.getReturnType()))
{
+                throw new ClassNotFoundException("newInstance() method does not return a
SessionFactory object!");
+            }
+
+            try {
+                result = (SessionFactory) newInstanceMethod.invoke(null, new Object[0]);
+            } catch (Exception e) {
+                throw new InstantiationException("Could not create SessionFactory object!");
+            }
+        } catch (NoSuchMethodException nsme) {
+            if (!SessionFactory.class.isAssignableFrom(clazz)) {
+                throw new ClassNotFoundException("The class does not implemnt the SessionFactory
interface!");
+            }
+
+            try {
+                result = (SessionFactory) clazz.newInstance();
+            } catch (Exception e) {
+                throw new InstantiationException("Could not create SessionFactory object!");
+            }
+        }
+
+        return result;
+    }
+}

Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/client/SessionFactoryFinderTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/client/SessionFactoryFinderTest.java?rev=1516624&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/client/SessionFactoryFinderTest.java
(added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/test/java/org/apache/chemistry/opencmis/client/SessionFactoryFinderTest.java
Thu Aug 22 21:39:58 2013
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client;
+
+import org.junit.Test;
+
+public class SessionFactoryFinderTest {
+
+    @Test(expected = ClassNotFoundException.class)
+    public void testFind() throws ClassNotFoundException, InstantiationException {
+        SessionFactoryFinder.find();
+    }
+}

Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/SessionFactoryFinderTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/SessionFactoryFinderTest.java?rev=1516624&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/SessionFactoryFinderTest.java
(added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/SessionFactoryFinderTest.java
Thu Aug 22 21:39:58 2013
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.runtime;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.chemistry.opencmis.client.SessionFactoryFinder;
+import org.apache.chemistry.opencmis.client.api.Repository;
+import org.apache.chemistry.opencmis.client.api.Session;
+import org.apache.chemistry.opencmis.client.api.SessionFactory;
+import org.junit.Test;
+
+public class SessionFactoryFinderTest {
+
+    @Test
+    public void testFind() throws ClassNotFoundException, InstantiationException {
+        SessionFactory sf = SessionFactoryFinder.find();
+
+        assertNotNull(sf);
+        assertTrue(sf instanceof SessionFactoryImpl);
+    }
+
+    @Test
+    public void testFindWithClassloader() throws ClassNotFoundException, InstantiationException
{
+        SessionFactory sf = SessionFactoryFinder.find(null, this.getClass().getClassLoader());
+
+        assertNotNull(sf);
+        assertTrue(sf instanceof SessionFactoryImpl);
+    }
+
+    @Test()
+    public void testFindWithProperty1() throws ClassNotFoundException, InstantiationException
{
+        System.setProperty("org.apache.chemistry.test.sessionfactory1", MockSessionFactory1.class.getName());
+
+        SessionFactory sf = SessionFactoryFinder.find("org.apache.chemistry.test.sessionfactory1");
+
+        assertNotNull(sf);
+        assertTrue(sf instanceof MockSessionFactory1);
+    }
+
+    @Test(expected = ClassNotFoundException.class)
+    public void testFindWithProperty2() throws ClassNotFoundException, InstantiationException
{
+        System.setProperty("org.apache.chemistry.test.sessionfactory2", SessionFactoryFinderTest.class.getName());
+
+        SessionFactoryFinder.find("org.apache.chemistry.test.sessionfactory2");
+    }
+
+    @Test()
+    public void testFindWithProperty3() throws ClassNotFoundException, InstantiationException
{
+        System.setProperty("org.apache.chemistry.test.sessionfactory3", MockSessionFactory2.class.getName());
+
+        SessionFactory sf = SessionFactoryFinder.find("org.apache.chemistry.test.sessionfactory3");
+
+        assertNotNull(sf);
+        assertTrue(sf instanceof MockSessionFactory2);
+    }
+
+    public static class MockSessionFactory1 implements SessionFactory {
+
+        public MockSessionFactory1() {
+        }
+
+        public Session createSession(Map<String, String> parameters) {
+            return null;
+        }
+
+        public List<Repository> getRepositories(Map<String, String> parameters)
{
+            return null;
+        }
+    }
+
+    public static class MockSessionFactory2 extends SessionFactoryImpl {
+
+        private MockSessionFactory2() {
+        }
+
+        public static MockSessionFactory2 newInstance() {
+            return new MockSessionFactory2();
+        }
+    }
+}



Mime
View raw message