Author: markt Date: Tue May 20 10:28:46 2014 New Revision: 1596186 URL: http://svn.apache.org/r1596186 Log: Experiment with the Simbian duplicate code detection tool. Remove 100+ lines of duplicate code
Added: tomcat/trunk/java/org/apache/naming/factory/FactoryBase.java (with props) Modified: tomcat/trunk/java/org/apache/naming/factory/EjbFactory.java tomcat/trunk/java/org/apache/naming/factory/ResourceEnvFactory.java tomcat/trunk/java/org/apache/naming/factory/ResourceFactory.java tomcat/trunk/java/org/apache/naming/factory/TransactionFactory.java Modified: tomcat/trunk/java/org/apache/naming/factory/EjbFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/EjbFactory.java?rev=1596186&r1=1596185&r2=1596186&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/naming/factory/EjbFactory.java (original) +++ tomcat/trunk/java/org/apache/naming/factory/EjbFactory.java Tue May 20 10:28:46 2014 @@ -16,11 +16,7 @@ */ package org.apache.naming.factory; -import java.util.Hashtable; - -import javax.naming.Context; import javax.naming.InitialContext; -import javax.naming.Name; import javax.naming.NamingException; import javax.naming.RefAddr; import javax.naming.Reference; @@ -33,96 +29,43 @@ import org.apache.naming.EjbRef; * * @author Remy Maucherat */ -public class EjbFactory implements ObjectFactory { +public class EjbFactory extends FactoryBase { + + @Override + protected boolean isReferenceTypeSupported(Object obj) { + return obj instanceof EjbRef; + } - /** - * Create a new EJB instance. - * - * @param obj The reference object describing the DataSource - */ @Override - public Object getObjectInstance(Object obj, Name name, Context nameCtx, - Hashtable<?,?> environment) - throws Exception { - - if (obj instanceof EjbRef) { - Reference ref = (Reference) obj; - - // If ejb-link has been specified, resolving the link using JNDI - RefAddr linkRefAddr = ref.get(EjbRef.LINK); - if (linkRefAddr != null) { - // Retrieving the EJB link - String ejbLink = linkRefAddr.getContent().toString(); - Object beanObj = (new InitialContext()).lookup(ejbLink); - return beanObj; - } - - ObjectFactory factory = null; - RefAddr factoryRefAddr = ref.get(Constants.FACTORY); - if (factoryRefAddr != null) { - // Using the specified factory - String factoryClassName = - factoryRefAddr.getContent().toString(); - // Loading factory - ClassLoader tcl = - Thread.currentThread().getContextClassLoader(); - Class<?> factoryClass = null; - if (tcl != null) { - try { - factoryClass = tcl.loadClass(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } else { - try { - factoryClass = Class.forName(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } - if (factoryClass != null) { - try { - factory = (ObjectFactory) factoryClass.newInstance(); - } catch(Throwable t) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(t); - throw ex; - } - } - } else { - String javaxEjbFactoryClassName = - System.getProperty("javax.ejb.Factory", - Constants.OPENEJB_EJB_FACTORY); - try { - factory = (ObjectFactory) - Class.forName(javaxEjbFactoryClassName).newInstance(); - } catch(Throwable t) { - if (t instanceof NamingException) - throw (NamingException) t; - NamingException ex = new NamingException - ("Could not create resource factory instance"); - ex.initCause(t); - throw ex; - } - } - - if (factory != null) { - return factory.getObjectInstance - (obj, name, nameCtx, environment); - } else { - throw new NamingException - ("Cannot create resource instance"); - } + protected ObjectFactory getDefaultFactory(Reference ref) throws NamingException { + ObjectFactory factory; + String javaxEjbFactoryClassName = System.getProperty( + "javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY); + try { + factory = (ObjectFactory) + Class.forName(javaxEjbFactoryClassName).newInstance(); + } catch(Throwable t) { + if (t instanceof NamingException) + throw (NamingException) t; + NamingException ex = new NamingException + ("Could not create resource factory instance"); + ex.initCause(t); + throw ex; } + return factory; + } + @Override + protected Object getLinked(Reference ref) throws NamingException { + // If ejb-link has been specified, resolving the link using JNDI + RefAddr linkRefAddr = ref.get(EjbRef.LINK); + if (linkRefAddr != null) { + // Retrieving the EJB link + String ejbLink = linkRefAddr.getContent().toString(); + Object beanObj = (new InitialContext()).lookup(ejbLink); + return beanObj; + } return null; } } Added: tomcat/trunk/java/org/apache/naming/factory/FactoryBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/FactoryBase.java?rev=1596186&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/naming/factory/FactoryBase.java (added) +++ tomcat/trunk/java/org/apache/naming/factory/FactoryBase.java Tue May 20 10:28:46 2014 @@ -0,0 +1,141 @@ +/* + * 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.naming.factory; + +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NamingException; +import javax.naming.RefAddr; +import javax.naming.Reference; +import javax.naming.spi.ObjectFactory; + +/** + * Abstract base class that provides common functionality required by + * sub-classes. This class exists primarily to reduce code duplication. + */ +public abstract class FactoryBase implements ObjectFactory { + + /** + * Creates a new object instance. + * + * @param obj The reference object describing the object to create + */ + @Override + public final Object getObjectInstance(Object obj, Name name, Context nameCtx, + Hashtable<?,?> environment) throws Exception { + + if (isReferenceTypeSupported(obj)) { + Reference ref = (Reference) obj; + + Object linked = getLinked(ref); + if (linked != null) { + return linked; + } + + ObjectFactory factory = null; + RefAddr factoryRefAddr = ref.get(Constants.FACTORY); + if (factoryRefAddr != null) { + // Using the specified factory + String factoryClassName = factoryRefAddr.getContent().toString(); + // Loading factory + ClassLoader tcl = Thread.currentThread().getContextClassLoader(); + Class<?> factoryClass = null; + if (tcl != null) { + try { + factoryClass = tcl.loadClass(factoryClassName); + } catch(ClassNotFoundException e) { + NamingException ex = new NamingException( + "Could not load resource factory class"); + ex.initCause(e); + throw ex; + } + } else { + try { + factoryClass = Class.forName(factoryClassName); + } catch(ClassNotFoundException e) { + NamingException ex = new NamingException( + "Could not load resource factory class"); + ex.initCause(e); + throw ex; + } + } + if (factoryClass != null) { + try { + factory = (ObjectFactory) factoryClass.newInstance(); + } catch(Throwable t) { + if (t instanceof NamingException) { + throw (NamingException) t; + } + NamingException ex = new NamingException( + "Could not create resource factory instance"); + ex.initCause(t); + throw ex; + } + } + } + + // Check for a default factory + factory = getDefaultFactory(ref); + + if (factory != null) { + return factory.getObjectInstance(obj, name, nameCtx, environment); + } else { + throw new NamingException("Cannot create resource instance"); + } + } + + return null; + } + + + /** + * Determines if this factory supports processing the provided reference + * object. + * + * @param obj The object to be processed + * + * @return <code>true</code> if this factory can process the object, + * otherwise <code>false</code> + */ + protected abstract boolean isReferenceTypeSupported(Object obj); + + /** + * If a default factory is available for the given reference type, create + * the default factory. + * + * @param ref The reference object to be processed + * + * @return The default factory for the given reference object or + * <code>null</code> if no default factory exists. + * + * @throws NamingException If the default factory can not be craeted + */ + protected abstract ObjectFactory getDefaultFactory(Reference ref) + throws NamingException; + + /** + * If this reference is a link to another JNDI object, obtain that object. + * + * @param ref The reference object to be processed + * + * @return The linked object or <code>null</code> if linked objects are + * not supported by or not configured for this reference object + */ + protected abstract Object getLinked(Reference ref) throws NamingException; +} Propchange: tomcat/trunk/java/org/apache/naming/factory/FactoryBase.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/java/org/apache/naming/factory/ResourceEnvFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/ResourceEnvFactory.java?rev=1596186&r1=1596185&r2=1596186&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/naming/factory/ResourceEnvFactory.java (original) +++ tomcat/trunk/java/org/apache/naming/factory/ResourceEnvFactory.java Tue May 20 10:28:46 2014 @@ -14,16 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - package org.apache.naming.factory; -import java.util.Hashtable; - -import javax.naming.Context; -import javax.naming.Name; -import javax.naming.NamingException; -import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; @@ -34,93 +26,22 @@ import org.apache.naming.ResourceEnvRef; * * @author Remy Maucherat */ -public class ResourceEnvFactory - implements ObjectFactory { - - - // ----------------------------------------------------------- Constructors - - - // -------------------------------------------------------------- Constants - - - // ----------------------------------------------------- Instance Variables - - - // --------------------------------------------------------- Public Methods +public class ResourceEnvFactory extends FactoryBase { - - // -------------------------------------------------- ObjectFactory Methods - - - /** - * Create a new Resource env instance. - * - * @param obj The reference object describing the DataSource - */ @Override - public Object getObjectInstance(Object obj, Name name, Context nameCtx, - Hashtable<?,?> environment) - throws Exception { - - if (obj instanceof ResourceEnvRef) { - Reference ref = (Reference) obj; - ObjectFactory factory = null; - RefAddr factoryRefAddr = ref.get(Constants.FACTORY); - if (factoryRefAddr != null) { - // Using the specified factory - String factoryClassName = - factoryRefAddr.getContent().toString(); - // Loading factory - ClassLoader tcl = - Thread.currentThread().getContextClassLoader(); - Class<?> factoryClass = null; - if (tcl != null) { - try { - factoryClass = tcl.loadClass(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } else { - try { - factoryClass = Class.forName(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } - if (factoryClass != null) { - try { - factory = (ObjectFactory) factoryClass.newInstance(); - } catch(Throwable t) { - if (t instanceof NamingException) - throw (NamingException) t; - NamingException ex = new NamingException - ("Could not create resource factory instance"); - ex.initCause(t); - throw ex; - } - } - } - // Note: No defaults here - if (factory != null) { - return factory.getObjectInstance - (obj, name, nameCtx, environment); - } else { - throw new NamingException - ("Cannot create resource instance"); - } - } + protected boolean isReferenceTypeSupported(Object obj) { + return obj instanceof ResourceEnvRef; + } + @Override + protected ObjectFactory getDefaultFactory(Reference ref) { + // No default factory supported. return null; - } - + @Override + protected Object getLinked(Reference ref) { + // Not supported + return null; + } } - Modified: tomcat/trunk/java/org/apache/naming/factory/ResourceFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/ResourceFactory.java?rev=1596186&r1=1596185&r2=1596186&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/naming/factory/ResourceFactory.java (original) +++ tomcat/trunk/java/org/apache/naming/factory/ResourceFactory.java Tue May 20 10:28:46 2014 @@ -14,16 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - package org.apache.naming.factory; -import java.util.Hashtable; - -import javax.naming.Context; -import javax.naming.Name; import javax.naming.NamingException; -import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; @@ -34,122 +27,52 @@ import org.apache.naming.ResourceRef; * * @author Remy Maucherat */ -public class ResourceFactory - implements ObjectFactory { - - - // ----------------------------------------------------------- Constructors - - - // -------------------------------------------------------------- Constants - - - // ----------------------------------------------------- Instance Variables - - - // --------------------------------------------------------- Public Methods +public class ResourceFactory extends FactoryBase { + @Override + protected boolean isReferenceTypeSupported(Object obj) { + return obj instanceof ResourceRef; + } - // -------------------------------------------------- ObjectFactory Methods + @Override + protected ObjectFactory getDefaultFactory(Reference ref) throws NamingException { + ObjectFactory factory = null; - /** - * Crete a new DataSource instance. - * - * @param obj The reference object describing the DataSource - */ - @Override - public Object getObjectInstance(Object obj, Name name, Context nameCtx, - Hashtable<?,?> environment) - throws Exception { - - if (obj instanceof ResourceRef) { - Reference ref = (Reference) obj; - ObjectFactory factory = null; - RefAddr factoryRefAddr = ref.get(Constants.FACTORY); - if (factoryRefAddr != null) { - // Using the specified factory - String factoryClassName = - factoryRefAddr.getContent().toString(); - // Loading factory - ClassLoader tcl = - Thread.currentThread().getContextClassLoader(); - Class<?> factoryClass = null; - if (tcl != null) { - try { - factoryClass = tcl.loadClass(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } else { - try { - factoryClass = Class.forName(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } - if (factoryClass != null) { - try { - factory = (ObjectFactory) factoryClass.newInstance(); - } catch (Exception e) { - if (e instanceof NamingException) - throw (NamingException) e; - NamingException ex = new NamingException - ("Could not create resource factory instance"); - ex.initCause(e); - throw ex; - } - } - } else { - if (ref.getClassName().equals("javax.sql.DataSource")) { - String javaxSqlDataSourceFactoryClassName = - System.getProperty("javax.sql.DataSource.Factory", - Constants.DBCP_DATASOURCE_FACTORY); - try { - factory = (ObjectFactory) - Class.forName(javaxSqlDataSourceFactoryClassName) - .newInstance(); - } catch (Exception e) { - NamingException ex = new NamingException - ("Could not create resource factory instance"); - ex.initCause(e); - throw ex; - } - } else if (ref.getClassName().equals("javax.mail.Session")) { - String javaxMailSessionFactoryClassName = - System.getProperty("javax.mail.Session.Factory", - "org.apache.naming.factory.MailSessionFactory"); - try { - factory = (ObjectFactory) - Class.forName(javaxMailSessionFactoryClassName) - .newInstance(); - } catch(Throwable t) { - NamingException ex = new NamingException - ("Could not create resource factory instance"); - ex.initCause(t); - throw ex; - } - } + if (ref.getClassName().equals("javax.sql.DataSource")) { + String javaxSqlDataSourceFactoryClassName = + System.getProperty("javax.sql.DataSource.Factory", + Constants.DBCP_DATASOURCE_FACTORY); + try { + factory = (ObjectFactory) Class.forName( + javaxSqlDataSourceFactoryClassName).newInstance(); + } catch (Exception e) { + NamingException ex = new NamingException( + "Could not create resource factory instance"); + ex.initCause(e); + throw ex; } - if (factory != null) { - return factory.getObjectInstance - (obj, name, nameCtx, environment); - } else { - throw new NamingException - ("Cannot create resource instance"); + } else if (ref.getClassName().equals("javax.mail.Session")) { + String javaxMailSessionFactoryClassName = + System.getProperty("javax.mail.Session.Factory", + "org.apache.naming.factory.MailSessionFactory"); + try { + factory = (ObjectFactory) + Class.forName(javaxMailSessionFactoryClassName).newInstance(); + } catch(Throwable t) { + NamingException ex = new NamingException( + "Could not create resource factory instance"); + ex.initCause(t); + throw ex; } } - return null; - + return factory; } - + @Override + protected Object getLinked(Reference ref) { + // Not supported + return null; + } } - Modified: tomcat/trunk/java/org/apache/naming/factory/TransactionFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/naming/factory/TransactionFactory.java?rev=1596186&r1=1596185&r2=1596186&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/naming/factory/TransactionFactory.java (original) +++ tomcat/trunk/java/org/apache/naming/factory/TransactionFactory.java Tue May 20 10:28:46 2014 @@ -14,16 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - package org.apache.naming.factory; -import java.util.Hashtable; - -import javax.naming.Context; -import javax.naming.Name; -import javax.naming.NamingException; -import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; @@ -34,93 +26,22 @@ import org.apache.naming.TransactionRef; * * @author Remy Maucherat */ -public class TransactionFactory - implements ObjectFactory { - - - // ----------------------------------------------------------- Constructors - - - // -------------------------------------------------------------- Constants - - - // ----------------------------------------------------- Instance Variables - - - // --------------------------------------------------------- Public Methods - +public class TransactionFactory extends FactoryBase { - // -------------------------------------------------- ObjectFactory Methods - - - /** - * Create a new User transaction instance. - * - * @param obj The reference object describing the DataSource - */ @Override - public Object getObjectInstance(Object obj, Name name, Context nameCtx, - Hashtable<?,?> environment) - throws Exception { - - if (obj instanceof TransactionRef) { - Reference ref = (Reference) obj; - ObjectFactory factory = null; - RefAddr factoryRefAddr = ref.get(Constants.FACTORY); - if (factoryRefAddr != null) { - // Using the specified factory - String factoryClassName = - factoryRefAddr.getContent().toString(); - // Loading factory - ClassLoader tcl = - Thread.currentThread().getContextClassLoader(); - Class<?> factoryClass = null; - if (tcl != null) { - try { - factoryClass = tcl.loadClass(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } else { - try { - factoryClass = Class.forName(factoryClassName); - } catch(ClassNotFoundException e) { - NamingException ex = new NamingException - ("Could not load resource factory class"); - ex.initCause(e); - throw ex; - } - } - if (factoryClass != null) { - try { - factory = (ObjectFactory) factoryClass.newInstance(); - } catch(Throwable t) { - if (t instanceof NamingException) - throw (NamingException) t; - NamingException ex = new NamingException - ("Could not create resource factory instance"); - ex.initCause(t); - throw ex; - } - } - } - if (factory != null) { - return factory.getObjectInstance - (obj, name, nameCtx, environment); - } else { - throw new NamingException - ("Cannot create resource instance"); - } - - } + protected boolean isReferenceTypeSupported(Object obj) { + return obj instanceof TransactionRef; + } + @Override + protected ObjectFactory getDefaultFactory(Reference ref) { + // No default factory supported. return null; - } - + @Override + protected Object getLinked(Reference ref) { + // Not supported + return null; + } } - --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org