[exo-jcr-commits] exo-jcr SVN: r2085 - in kernel/trunk/exo.kernel.component.common/src: test/java/org/exoplatform/services/naming and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Mar 19 11:19:01 EDT 2010


Author: tolusha
Date: 2010-03-19 11:19:01 -0400 (Fri, 19 Mar 2010)
New Revision: 2085

Removed:
   kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/naming/TestInitialContextBinder.java
Modified:
   kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/InitialContextBinder.java
   kernel/trunk/exo.kernel.component.common/src/test/resources/conf/portal/test-configuration.xml
Log:
EXOJCR-573: InitContextBinder small refactoring

Modified: kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/InitialContextBinder.java
===================================================================
--- kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/InitialContextBinder.java	2010-03-19 07:22:38 UTC (rev 2084)
+++ kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/naming/InitialContextBinder.java	2010-03-19 15:19:01 UTC (rev 2085)
@@ -19,9 +19,6 @@
 package org.exoplatform.services.naming;
 
 import org.exoplatform.container.configuration.ConfigurationException;
-import org.exoplatform.container.xml.InitParams;
-import org.exoplatform.container.xml.PropertiesParam;
-import org.exoplatform.container.xml.ValueParam;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -41,7 +38,6 @@
 import javax.naming.RefAddr;
 import javax.naming.Reference;
 import javax.naming.StringRefAddr;
-import javax.sql.DataSource;
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLOutputFactory;
@@ -54,6 +50,8 @@
 import javax.xml.stream.events.XMLEvent;
 
 /**
+ * Class is responsible for binding datasources at runtime, persists on file and automatically rebinds after restart.
+ * 
  * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
  * @version $Id: InitialContextBinder.java 111 2010-11-11 11:11:11Z tolusha $
  */
@@ -81,10 +79,8 @@
     */
    protected final InitialContextInitializer initialContextInitializer;
 
-   protected final String bindReferencesStorage;
+   protected final String bindReferencesPath;
 
-   protected final Reference reference;
-
    protected Map<String, Reference> bindReferences;
 
    /**
@@ -100,41 +96,16 @@
     * @throws XMLStreamException
     * @throws NamingException
     */
-   public InitialContextBinder(InitialContextInitializer initialContextInitializer, InitParams initParams)
-      throws ConfigurationException, FileNotFoundException, XMLStreamException, NamingException
+   public InitialContextBinder(InitialContextInitializer initialContextInitializer) throws ConfigurationException,
+      FileNotFoundException, XMLStreamException, NamingException
    {
       this.initialContextInitializer = initialContextInitializer;
 
-      ValueParam cnParam = initParams.getValueParam("class-name");
-      if (cnParam == null)
-      {
-         throw new ConfigurationException("class-name parameter expected");
-      }
+      this.bindReferences = new HashMap<String, Reference>();
+      this.bindReferencesPath = System.getProperty("java.io.tmpdir") + File.separator + "datasources.xml";
 
-      ValueParam factoryParam = initParams.getValueParam("factory");
-      if (factoryParam == null)
+      if (new File(bindReferencesPath).exists())
       {
-         throw new ConfigurationException("factory parameter expected");
-      }
-
-      ValueParam flParam = initParams.getValueParam("factory-location");
-      String factoryLocation = flParam != null ? flParam.getValue() : null;
-
-      this.reference = new Reference(cnParam.getValue(), factoryParam.getValue(), factoryLocation);
-
-      PropertiesParam addrsParam = initParams.getPropertiesParam("ref-addresses");
-      if (addrsParam != null)
-      {
-         for (Entry entry : addrsParam.getProperties().entrySet())
-         {
-            reference.add(new StringRefAddr((String)entry.getKey(), (String)entry.getValue()));
-         }
-      }
-
-      this.bindReferencesStorage = System.getProperty("java.io.tmpdir") + File.separator + "datasources.xml";
-      this.bindReferences = new HashMap<String, Reference>();
-      if (new File(bindReferencesStorage).exists())
-      {
          this.bindReferences.putAll(doImport());
          for (Entry<String, Reference> entry : bindReferences.entrySet())
          {
@@ -144,24 +115,21 @@
    }
 
    /**
-    * Bind reference and return datasource.
-    * 
-    * @param bindName
-    *          bind name
-    * @return  datasource
-    * @throws NamingException
-    * @throws FileNotFoundException
-    * @throws XMLStreamException
+    * Bind reference.
     */
-   public DataSource bind(String bindName) throws NamingException, FileNotFoundException, XMLStreamException
+   public void bind(String bindName, String className, String factory, String factoryLocation,
+      Map<String, String> refAddr) throws NamingException, FileNotFoundException, XMLStreamException
    {
+      Reference reference = new Reference(className, factory, factoryLocation);
+      for (Entry entry : refAddr.entrySet())
+      {
+         reference.add(new StringRefAddr((String)entry.getKey(), (String)entry.getValue()));
+      }
+
       bind(bindName, reference);
 
       bindReferences.put(bindName, reference);
       doExport();
-
-      return (DataSource)initialContextInitializer.getInitialContext().lookup(bindName);
-
    }
 
    private void bind(String bindName, Reference reference) throws NamingException
@@ -170,7 +138,7 @@
    }
 
    /**
-    * Export into xml.
+    * Export references into xml-file.
     * 
     * @throws XMLStreamException
     * @throws FileNotFoundException
@@ -178,8 +146,7 @@
    protected void doExport() throws XMLStreamException, FileNotFoundException
    {
       XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
-      XMLStreamWriter writer =
-         outputFactory.createXMLStreamWriter(new FileOutputStream(bindReferencesStorage), "UTF-8");
+      XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileOutputStream(bindReferencesPath), "UTF-8");
 
       writer.writeStartDocument("UTF-8", "1.0");
       writer.writeStartElement(BIND_REFERENCES_ELEMENT);
@@ -220,9 +187,10 @@
    }
 
    /**
-    * Import from xml. 
+    * Import references from xml-file. 
     * 
-    * @return
+    * @return list of references
+    * 
     * @throws XMLStreamException 
     * @throws FileNotFoundException 
     */
@@ -231,7 +199,7 @@
       Map<String, Reference> references = new HashMap<String, Reference>();
 
       XMLInputFactory factory = XMLInputFactory.newInstance();
-      XMLEventReader reader = factory.createXMLEventReader(new FileInputStream(bindReferencesStorage), "UTF-8");
+      XMLEventReader reader = factory.createXMLEventReader(new FileInputStream(bindReferencesPath), "UTF-8");
 
       while (reader.hasNext())
       {

Deleted: kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/naming/TestInitialContextBinder.java
===================================================================
--- kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/naming/TestInitialContextBinder.java	2010-03-19 07:22:38 UTC (rev 2084)
+++ kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/naming/TestInitialContextBinder.java	2010-03-19 15:19:01 UTC (rev 2085)
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2010 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * 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.
- *
- * This software 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.exoplatform.services.naming;
-
-import org.exoplatform.container.PortalContainer;
-
-import junit.framework.TestCase;
-
-import javax.sql.DataSource;
-
-/**
- * @author <a href="anatoliy.bazko at exoplatform.org">Anatoliy Bazko</a>
- * @version $Id: TestInitialContextBinder.java 111 2010-11-11 11:11:11Z tolusha $
- */
-public class TestInitialContextBinder extends TestCase
-{
-   private InitialContextBinder initialContextBinder;
-
-   public void setUp() throws Exception
-   {
-      PortalContainer container = PortalContainer.getInstance();
-
-      InitialContextInitializer initializer =
-         (InitialContextInitializer)container.getComponentInstanceOfType(InitialContextInitializer.class);
-      initialContextBinder = (InitialContextBinder)container.getComponentInstanceOfType(InitialContextBinder.class);
-   }
-
-   public void testBind() throws Exception
-   {
-      DataSource ds = initialContextBinder.bind("testjdbcjcr");
-      assertNotNull(ds);
-   }
-
-}

Modified: kernel/trunk/exo.kernel.component.common/src/test/resources/conf/portal/test-configuration.xml
===================================================================
--- kernel/trunk/exo.kernel.component.common/src/test/resources/conf/portal/test-configuration.xml	2010-03-19 07:22:38 UTC (rev 2084)
+++ kernel/trunk/exo.kernel.component.common/src/test/resources/conf/portal/test-configuration.xml	2010-03-19 15:19:01 UTC (rev 2085)
@@ -48,24 +48,6 @@
    <component>
       <key>org.exoplatform.services.naming.InitialContextBinder</key>
       <type>org.exoplatform.services.naming.InitialContextBinder</type>
-        <init-params>
-           <value-param>
-              <name>class-name</name>
-              <value>javax.sql.DataSource</value>
-           </value-param>
-           <value-param>
-              <name>factory</name>
-              <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
-           </value-param>
-           <properties-param>
-              <name>ref-addresses</name>
-              <description>ref-addresses</description>
-              <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
-              <property name="url" value="jdbc:hsqldb:file:target/temp/data/portal" />
-              <property name="username" value="sa" />
-              <property name="password" value="" />
-           </properties-param>
-        </init-params>
    </component>
 
    <component>



More information about the exo-jcr-commits mailing list