You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2006/07/11 11:49:11 UTC
svn commit: r420777 - in /xmlgraphics/commons/trunk:
src/java/org/apache/xmlgraphics/util/Service.java
test/java/org/apache/xmlgraphics/StandardTestSuite.java
test/java/org/apache/xmlgraphics/util/ServiceTest.java
Author: jeremias
Date: Tue Jul 11 02:49:10 2006
New Revision: 420777
URL: http://svn.apache.org/viewvc?rev=420777&view=rev
Log:
Add an optional parameter to the Service class so it can also return class names instead of (singleton) instances. This is to resurrect the behaviour we had in FOP and to fix a thread-safety issue in FOP.
Test case added to test the expected behaviour.
Added:
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/ServiceTest.java (with props)
Modified:
xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/Service.java
xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java
Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/Service.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/Service.java?rev=420777&r1=420776&r2=420777&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/Service.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/Service.java Tue Jul 11 02:49:10 2006
@@ -43,7 +43,8 @@
public class Service {
// Remember providers we have looked up before.
- static Map providerMap = new java.util.HashMap();
+ static Map classMap = new java.util.HashMap();
+ static Map instanceMap = new java.util.HashMap();
/**
* Returns an iterator where each element should implement the
@@ -57,15 +58,32 @@
* @param cls The class/interface to search for providers of.
*/
public static synchronized Iterator providers(Class cls) {
+ return providers(cls, true);
+ }
+
+ /**
+ * Returns an iterator where each element should implement the
+ * interface (or subclass the baseclass) described by cls. The
+ * Classes are found by searching the classpath for service files
+ * named: 'META-INF/services/<fully qualified classname> that list
+ * fully qualifted classnames of classes that implement the
+ * service files classes interface. These classes must have
+ * default constructors if returnInstances is true.
+ *
+ * @param cls The class/interface to search for providers of.
+ * @param returnInstances true if the iterator should return instances rather than class names.
+ */
+ public static synchronized Iterator providers(Class cls, boolean returnInstances) {
String serviceFile = "META-INF/services/" + cls.getName();
+ Map cacheMap = (returnInstances ? instanceMap : classMap);
- List l = (List)providerMap.get(serviceFile);
+ List l = (List)cacheMap.get(serviceFile);
if (l != null) {
return l.iterator();
}
l = new java.util.ArrayList();
- providerMap.put(serviceFile, l);
+ cacheMap.put(serviceFile, l);
ClassLoader cl = null;
try {
@@ -112,10 +130,14 @@
continue;
}
- // Try and load the class
- Object obj = cl.loadClass(line).newInstance();
- // stick it into our vector...
- l.add(obj);
+ if (returnInstances) {
+ // Try and load the class
+ Object obj = cl.loadClass(line).newInstance();
+ // stick it into our vector...
+ l.add(obj);
+ } else {
+ l.add(line);
+ }
} catch (Exception ex) {
// Just try the next line
}
Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java?rev=420777&r1=420776&r2=420777&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java Tue Jul 11 02:49:10 2006
@@ -19,6 +19,7 @@
package org.apache.xmlgraphics;
import org.apache.xmlgraphics.image.codec.png.PNGEncoderTest;
+import org.apache.xmlgraphics.util.ServiceTest;
import org.apache.xmlgraphics.util.io.ASCII85InputStreamTestCase;
import org.apache.xmlgraphics.util.io.ASCII85OutputStreamTestCase;
import org.apache.xmlgraphics.util.io.Base64Test;
@@ -43,6 +44,7 @@
suite.addTest(new TestSuite(ASCII85InputStreamTestCase.class));
suite.addTest(new TestSuite(ASCII85OutputStreamTestCase.class));
suite.addTest(new TestSuite(PNGEncoderTest.class));
+ suite.addTest(new TestSuite(ServiceTest.class));
//$JUnit-END$
return suite;
}
Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/ServiceTest.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/ServiceTest.java?rev=420777&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/ServiceTest.java (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/ServiceTest.java Tue Jul 11 02:49:10 2006
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+/* $Id$ */
+
+package org.apache.xmlgraphics.util;
+
+import java.util.Iterator;
+
+import org.apache.xmlgraphics.image.writer.ImageWriter;
+
+import junit.framework.TestCase;
+
+/**
+ * Test for the Service class.
+ */
+public class ServiceTest extends TestCase {
+
+ /**
+ * Tests the mode where Service returns instances.
+ * @throws Exception in case of an error
+ */
+ public void testWithInstances() throws Exception {
+ Class cls = ImageWriter.class;
+ boolean found = false;
+ Object writer1 = null;
+ Object writer2 = null;
+
+ //First run: Find a writer implementation (one of the two must be available)
+ Iterator iter = Service.providers(cls);
+ while (iter.hasNext()) {
+ Object obj = iter.next();
+ assertNotNull(obj);
+ String className = obj.getClass().getName();
+ if ("org.apache.xmlgraphics.image.writer.internal.PNGImageWriter".equals(className)) {
+ writer1 = obj;
+ found = true;
+ break;
+ } else if ("org.apache.xmlgraphics.image.writer.imageio.ImageIOPNGImageWriter".equals(
+ className)) {
+ writer2 = obj;
+ found = true;
+ break;
+ }
+ }
+ assertTrue("None of the expected classes found", found);
+
+ //Second run: verify that the same instances are returned
+ iter = Service.providers(cls);
+ while (iter.hasNext()) {
+ Object obj = iter.next();
+ assertNotNull(obj);
+ String className = obj.getClass().getName();
+ if ("org.apache.xmlgraphics.image.writer.internal.PNGImageWriter".equals(className)) {
+ assertTrue(obj == writer1);
+ break;
+ } else if ("org.apache.xmlgraphics.image.writer.imageio.ImageIOPNGImageWriter".equals(
+ className)) {
+ assertTrue(obj == writer2);
+ break;
+ }
+ }
+ }
+
+ /**
+ * Tests the mode where Service returns class names.
+ * @throws Exception in case of an error
+ */
+ public void testWithClassNames() throws Exception {
+ Class cls = ImageWriter.class;
+ boolean found = true;
+ Iterator iter = Service.providers(cls, false);
+ while (iter.hasNext()) {
+ Object obj = iter.next();
+ assertNotNull(obj);
+ assertTrue("Returned object must be a class name", obj instanceof String);
+ if ("org.apache.xmlgraphics.image.writer.internal.PNGImageWriter".equals(obj)
+ || ("org.apache.xmlgraphics.image.writer.imageio.ImageIOPNGImageWriter".equals(
+ obj))) {
+ found = true;
+ }
+ }
+ assertTrue("None of the expected classes found", found);
+
+ //Do it a second time to make sure the cache works as expected
+ iter = Service.providers(cls, false);
+ while (iter.hasNext()) {
+ Object obj = iter.next();
+ assertNotNull(obj);
+ assertTrue("Returned object must be a class name", obj instanceof String);
+ }
+ }
+
+}
Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/util/ServiceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org