Author: davsclaus
Date: Mon Sep 24 11:40:04 2012
New Revision: 1389304
URL: http://svn.apache.org/viewvc?rev=1389304&view=rev
Log:
CAMEL-5611: Added singleton based jndi context factory. Can be used for testing
purposes etc. Thanks to Benjamin Graf for patch.
Added:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelSingletonInitialContextFactory.java
- copied, changed from r1389287,
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java
(with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java?rev=1389304&r1=1389303&r2=1389304&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
Mon Sep 24 11:40:04 2012
@@ -17,7 +17,6 @@
package org.apache.camel.util.jndi;
import java.util.Hashtable;
-
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
@@ -25,8 +24,11 @@ import javax.naming.spi.InitialContextFa
import org.apache.camel.util.CastUtils;
/**
- * A factory of the Camel InitialContext which allows a Map to be used to
create a
+ * A factory of the Camel {@link javax.naming.InitialContext} which allows a
{@link java.util.Map} to be used to create a
* JNDI context.
+ * <p/>
+ * This implementation is prototype based, by creating a <b>new</b> context on
each call to
+ * {@link #getInitialContext(java.util.Hashtable)}.
*
* @version
*/
Copied:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelSingletonInitialContextFactory.java
(from r1389287,
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelSingletonInitialContextFactory.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelSingletonInitialContextFactory.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java&r1=1389287&r2=1389304&rev=1389304&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelInitialContextFactory.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/util/jndi/CamelSingletonInitialContextFactory.java
Mon Sep 24 11:40:04 2012
@@ -17,38 +17,36 @@
package org.apache.camel.util.jndi;
import java.util.Hashtable;
-
import javax.naming.Context;
import javax.naming.NamingException;
-import javax.naming.spi.InitialContextFactory;
-
-import org.apache.camel.util.CastUtils;
/**
- * A factory of the Camel InitialContext which allows a Map to be used to
create a
+ * A factory of the Camel {@link javax.naming.InitialContext} which allows a
{@link java.util.Map} to be used to create a
* JNDI context.
+ * <p/>
+ * This implementation is singleton based, by creating a <b>new</b> context
once, and reusing it on each call to
+ * {@link #getInitialContext(java.util.Hashtable)}.
*
- * @version
+ * @version
*/
-public class CamelInitialContextFactory implements InitialContextFactory {
+public class CamelSingletonInitialContextFactory extends
CamelInitialContextFactory {
+
+ private static volatile Context context;
/**
- * Creates a new context with the given environment.
+ * Gets or creates the context with the given environment.
+ * <p/>
+ * This implementation will create the context once, and then return the
same instance
+ * on multiple calls.
*
* @param environment the environment, must not be <tt>null</tt>
* @return the created context.
- * @throws NamingException is thrown if creation failed.
+ * @throws javax.naming.NamingException is thrown if creation failed.
*/
- public Context getInitialContext(Hashtable<?, ?> environment) throws
NamingException {
- try {
- return new JndiContext(CastUtils.cast(environment, String.class,
Object.class));
- } catch (Exception e) {
- if (e instanceof NamingException) {
- throw (NamingException) e;
- }
- NamingException exception = new NamingException(e.getMessage());
- exception.initCause(e);
- throw exception;
+ public synchronized Context getInitialContext(Hashtable<?, ?> environment)
throws NamingException {
+ if (context == null) {
+ context = super.getInitialContext(environment);
}
+ return context;
}
}
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java?rev=1389304&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java
Mon Sep 24 11:40:04 2012
@@ -0,0 +1,98 @@
+/**
+ * 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.camel.util.jndi;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.util.FileUtil;
+
+/**
+ *
+ */
+public class JndiCamelSingletonInitialContextFactoryTest extends
ContextTestSupport {
+
+ private static final String FAKE = "!!! Get DataSource fake !!!";
+ private File file = new File("src/test/resources/jndi.properties");
+
+ @Override
+ protected void setUp() throws Exception {
+ FileUtil.deleteFile(file);
+
+ // crete jndi.properties file
+ FileOutputStream fos = new FileOutputStream(file);
+ try {
+ String name = "java.naming.factory.initial=" +
CamelSingletonInitialContextFactory.class.getName();
+ fos.write(name.getBytes());
+ } finally {
+ fos.close();
+ }
+
+ super.setUp();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ FileUtil.deleteFile(file);
+ super.tearDown();
+ }
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ // create jndi registry to use in Camel, using the default initial
contex (which will read jndi.properties)
+ // (instead of what test-support offers normally)
+ JndiRegistry jndi = new JndiRegistry(new InitialContext());
+ jndi.bind("jdbc/myDataSource", FAKE);
+ return jndi;
+ }
+
+ public void testSingletonJndiContext() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived(FAKE);
+
+ template.sendBody("direct:simple", "Dummy");
+
+ mock.assertIsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:simple")
+ .process(new Processor() {
+ @Override
+ public void process(Exchange exchange) throws
Exception {
+ // calling this should get us the existing
context
+ Context context = new InitialContext();
+
exchange.getIn().setBody(context.lookup("jdbc/myDataSource").toString());
+ }
+ })
+ .to("mock:result");
+ }
+ };
+ }
+}
Propchange:
camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/camel-core/src/test/java/org/apache/camel/util/jndi/JndiCamelSingletonInitialContextFactoryTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date