diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-06-12 18:04:02 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-06-12 18:04:02 +0000 |
commit | 8e073f2bcb612cd19f45f8222d7c543309f813e5 (patch) | |
tree | 039dbc0320f227bcfd152fe72dcca89aa7e633d9 /java | |
parent | 05da59338aeec9b2362e7369f5e1f1836ccbc375 (diff) |
Fix concurrency issue in JAXBDataSource (TUSCANY-3065)
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@784222 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
4 files changed, 61 insertions, 49 deletions
diff --git a/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXB2OMElement.java b/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXB2OMElement.java index 9b11e2ff11..e0e0c08f6c 100644 --- a/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXB2OMElement.java +++ b/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXB2OMElement.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.databinding.jaxb.axiom; @@ -34,10 +34,14 @@ import org.apache.tuscany.sca.databinding.jaxb.JAXBDataBinding; /** * JAXB Object --> AXIOM OMElement transformer - * + * * @version $Rev$ $Date$ */ public class JAXB2OMElement extends BaseTransformer<Object, OMElement> implements PullTransformer<Object, OMElement> { + public JAXB2OMElement() { + super(); + } + private OMFactory factory = OMAbstractFactory.getOMFactory(); @Override diff --git a/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java b/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java index b6fa362ed9..cc28d5e710 100644 --- a/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java +++ b/java/sca/modules/databinding-jaxb-axiom/src/main/java/org/apache/tuscany/sca/databinding/jaxb/axiom/JAXBDataSource.java @@ -6,18 +6,21 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.databinding.jaxb.axiom; +import static org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper.getMarshaller; +import static org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper.releaseJAXBMarshaller; + import java.io.OutputStream; import java.io.StringReader; import java.io.StringWriter; @@ -27,7 +30,6 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -36,7 +38,6 @@ import javax.xml.stream.XMLStreamWriter; import org.apache.axiom.om.OMDataSource; import org.apache.axiom.om.OMOutputFormat; import org.apache.axiom.om.util.StAXUtils; -import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper; /** * @@ -45,31 +46,20 @@ import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper; public class JAXBDataSource implements OMDataSource { private JAXBContext context; private Object element; - private Marshaller marshaller; + // private Marshaller marshaller; public JAXBDataSource(Object element, JAXBContext context) { this.element = element; this.context = context; } - private Marshaller getMarshaller() throws JAXBException { - if (marshaller == null) { - // For thread safety, not sure we can cache the marshaller - marshaller = JAXBContextHelper.getMarshaller(context); - } - return marshaller; - } - - private void releaseMarshaller(Marshaller marshaller) { - JAXBContextHelper.releaseJAXBMarshaller(context, marshaller); - } - public XMLStreamReader getReader() throws XMLStreamException { // FIXME: [rfeng] This is a quick and dirty implementation // We could use the fastinfoset to optimize the roundtrip StringWriter writer = new StringWriter(); serialize(writer, new OMOutputFormat()); StringReader reader = new StringReader(writer.toString()); + // FIXME: We need to use Tuscany extension point to create the reader return StAXUtils.createXMLStreamReader(reader); } @@ -78,12 +68,13 @@ public class JAXBDataSource implements OMDataSource { // marshaller.setProperty(Marshaller.JAXB_ENCODING, format.getCharSetEncoding()); AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { + Marshaller marshaller = null; try { - Marshaller marshaller = getMarshaller(); - marshaller.marshal(element, xmlWriter); + marshaller = getMarshaller(context); + marshaller.marshal(element, xmlWriter); } finally { - releaseMarshaller(marshaller); - } + releaseJAXBMarshaller(context, marshaller); + } return null; } }); @@ -97,11 +88,12 @@ public class JAXBDataSource implements OMDataSource { // marshaller.setProperty(Marshaller.JAXB_ENCODING, format.getCharSetEncoding()); AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { + Marshaller marshaller = null; try { - Marshaller marshaller = getMarshaller(); + marshaller = getMarshaller(context); marshaller.marshal(element, output); } finally { - releaseMarshaller(marshaller); + releaseJAXBMarshaller(context, marshaller); } return null; } @@ -115,11 +107,12 @@ public class JAXBDataSource implements OMDataSource { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { + Marshaller marshaller = null; try { - Marshaller marshaller = getMarshaller(); + marshaller = getMarshaller(context); marshaller.marshal(element, writer); } finally { - releaseMarshaller(marshaller); + releaseJAXBMarshaller(context, marshaller); } return null; } @@ -128,7 +121,7 @@ public class JAXBDataSource implements OMDataSource { throw new XMLStreamException(e.getException()); } } - + public Object getObject() { return element; } diff --git a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultTransformerExtensionPoint.java b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultTransformerExtensionPoint.java index fc8b89c5b9..6258465c13 100644 --- a/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultTransformerExtensionPoint.java +++ b/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultTransformerExtensionPoint.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.databinding; @@ -27,6 +27,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.databinding.impl.DirectedGraph; import org.apache.tuscany.sca.extensibility.ServiceDeclaration; import org.apache.tuscany.sca.extensibility.ServiceDiscovery; @@ -38,9 +39,11 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi private static final Logger logger = Logger.getLogger(DefaultTransformerExtensionPoint.class.getName()); private boolean loadedTransformers; + private ExtensionPointRegistry registry; private final DirectedGraph<Object, Transformer> graph = new DirectedGraph<Object, Transformer>(); - public DefaultTransformerExtensionPoint() { + public DefaultTransformerExtensionPoint(ExtensionPointRegistry registry) { + this.registry = registry; } public void addTransformer(String sourceType, String resultType, int weight, Transformer transformer, boolean publicTransformer) { @@ -101,12 +104,12 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi loadedTransformers = true; loadTransformers(PullTransformer.class); loadTransformers(PushTransformer.class); - + } /** * Dynamically load transformers registered under META-INF/services. - * + * * @param transformerClass */ private synchronized void loadTransformers(Class<?> transformerClass) { @@ -149,7 +152,7 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi * A transformer facade allowing transformers to be lazily loaded * and initialized. */ - private static class LazyPullTransformer implements PullTransformer<Object, Object> { + private class LazyPullTransformer implements PullTransformer<Object, Object> { private String source; private String target; @@ -166,7 +169,7 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi /** * Load and instantiate the transformer class. - * + * * @return The transformer. */ @SuppressWarnings("unchecked") @@ -175,8 +178,14 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi try { Class<PullTransformer<Object, Object>> transformerClass = (Class<PullTransformer<Object, Object>>)transformerDeclaration.loadClass(); - Constructor<PullTransformer<Object, Object>> constructor = transformerClass.getConstructor(); - transformer = constructor.newInstance(); + try { + Constructor<PullTransformer<Object, Object>> constructor = transformerClass.getConstructor(); + transformer = constructor.newInstance(); + } catch (NoSuchMethodException e) { + Constructor<PullTransformer<Object, Object>> constructor = + transformerClass.getConstructor(ExtensionPointRegistry.class); + transformer = constructor.newInstance(registry); + } } catch (Exception e) { throw new IllegalStateException(e); } @@ -212,7 +221,7 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi * A transformer facade allowing transformers to be lazily loaded * and initialized. */ - private static class LazyPushTransformer implements PushTransformer<Object, Object> { + private class LazyPushTransformer implements PushTransformer<Object, Object> { private String source; private String target; @@ -229,7 +238,7 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi /** * Load and instantiate the transformer class. - * + * * @return The transformer. */ @SuppressWarnings("unchecked") @@ -238,8 +247,14 @@ public class DefaultTransformerExtensionPoint implements TransformerExtensionPoi try { Class<PushTransformer<Object, Object>> transformerClass = (Class<PushTransformer<Object, Object>>)transformerDeclaration.loadClass(); - Constructor<PushTransformer<Object, Object>> constructor = transformerClass.getConstructor(); - transformer = constructor.newInstance(); + try { + Constructor<PushTransformer<Object, Object>> constructor = transformerClass.getConstructor(); + transformer = constructor.newInstance(); + } catch (NoSuchMethodException e) { + Constructor<PushTransformer<Object, Object>> constructor = + transformerClass.getConstructor(ExtensionPointRegistry.class); + transformer = constructor.newInstance(registry); + } } catch (Exception e) { throw new IllegalStateException(e); } diff --git a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java index 3bb1ed7376..0be7320f3e 100644 --- a/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java +++ b/java/sca/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java @@ -6,15 +6,15 @@ * 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. + * under the License. */ package org.apache.tuscany.sca.databinding.impl; @@ -80,7 +80,7 @@ public class MediatorImplTestCase { @Before public void setUp() throws Exception { DataBindingExtensionPoint dataBindingRegistry = new DefaultDataBindingExtensionPoint(); - TransformerExtensionPoint registry = new DefaultTransformerExtensionPoint(); + TransformerExtensionPoint registry = new DefaultTransformerExtensionPoint(null); registry.addTransformer(new String2SAX(), true); registry.addTransformer(new SAX2DOMPipe(), true); |