cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r956597 - in /cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb: InMemoryLRUMarshallerCache.java JAXBGenerator.java
Date Mon, 21 Jun 2010 14:21:49 GMT
Author: simonetripodi
Date: Mon Jun 21 14:21:49 2010
New Revision: 956597

URL: http://svn.apache.org/viewvc?rev=956597&view=rev
Log:
optimized the jaxb Marshaller instances creation using a memory-based LRU cache

Added:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
  (with props)
Modified:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java

Added: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java?rev=956597&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
Mon Jun 21 14:21:49 2010
@@ -0,0 +1,96 @@
+/*
+ * 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.cocoon.optional.pipeline.components.sax.jaxb;
+
+import java.io.Serializable;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+
+/**
+ * @version $Id$
+ */
+final class InMemoryLRUMarshallerCache implements Serializable {
+
+    /**
+     * This class serial version UID.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The fixed cache size.
+     */
+    private static final int CACHE_SIZE = 255;
+
+    /**
+     * The fixed cache load factor.
+     */
+    private static final float LOAD_FACTOR = 0.75f;
+
+    /**
+     * The fixed cache capacity.
+     */
+    private static final int CACHE_CAPACITY = (int) Math.ceil(CACHE_SIZE / LOAD_FACTOR) +
1;
+
+    /**
+     * The map that implements the LRU cache.
+     */
+    private final Map<Class<?>, JAXBContext> data = new LinkedHashMap<Class<?>,
JAXBContext>(CACHE_CAPACITY, LOAD_FACTOR) {
+        /**
+         * This class serialVersionUID.
+         */
+        private static final long serialVersionUID = 1L;
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        protected boolean removeEldestEntry(Map.Entry<Class<?>, JAXBContext>
eldest) {
+            return size() > CACHE_SIZE;
+        }
+    };
+
+    /**
+     * Creates the JAXBContext and stores it if the class parameter has never
+     * previously analyzed, otherwise gets it from a local cache; finally it uses
+     * it to create the Marshaller.
+     *
+     * @param clazz the class for which the Marshaller has to be created.
+     * @return the JAXB Marshaller.
+     * @throws JAXBException if any error occurs.
+     */
+    public Marshaller getMarshaller(Class<?> clazz) throws JAXBException {
+        if (clazz == null) {
+            throw new IllegalArgumentException("Parameter 'clazz' must not be null");
+        }
+
+        JAXBContext jaxbContext = null;
+
+        if (this.data.containsKey(clazz)) {
+            jaxbContext = this.data.get(clazz);
+        } else {
+            jaxbContext = JAXBContext.newInstance(clazz);
+            this.data.put(clazz, jaxbContext);
+        }
+
+        return jaxbContext.createMarshaller();
+    }
+
+}

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/InMemoryLRUMarshallerCache.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java?rev=956597&r1=956596&r2=956597&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java
Mon Jun 21 14:21:49 2010
@@ -18,7 +18,6 @@
  */
 package org.apache.cocoon.optional.pipeline.components.sax.jaxb;
 
-import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 
@@ -34,6 +33,8 @@ import org.apache.cocoon.sax.AbstractSAX
  */
 public final class JAXBGenerator extends AbstractSAXGenerator implements CachingPipelineComponent
{
 
+    private static final InMemoryLRUMarshallerCache MARSHALLER_CACHE = new InMemoryLRUMarshallerCache();
+
     private final Object toBeMarshalled;
 
     public JAXBGenerator(Object toBeMarshalled) {
@@ -45,8 +46,7 @@ public final class JAXBGenerator extends
 
     public void execute() {
         try {
-            JAXBContext jaxbContext = JAXBContext.newInstance(this.toBeMarshalled.getClass());
-            Marshaller xmlMarshaller = jaxbContext.createMarshaller();
+            Marshaller xmlMarshaller = MARSHALLER_CACHE.getMarshaller(this.toBeMarshalled.getClass());
             xmlMarshaller.marshal(this.toBeMarshalled, this.getSAXConsumer());
         } catch (JAXBException e) {
             throw new ProcessingException("Impossible to marshal object of type "



Mime
View raw message