summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextCache.java
blob: 5ddf7d3604c82d218923b08a0d512d781bff5d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * 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.tuscany.sca.databinding.jaxb;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.SoftReference;
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import javax.activation.DataHandler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.transform.Source;

import org.apache.tuscany.sca.common.java.collection.LRUCache;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.extensibility.ClassLoaderContext;
import org.oasisopen.sca.ServiceRuntimeException;

/**
 * @version $Rev$ $Date$
 */
public class JAXBContextCache {
    private static final int CACHE_SIZE = 128;

    private static HashMap<String, Class<?>> loadClassMap = new HashMap<String, Class<?>>();

    static {
        loadClassMap.put("byte", byte.class);
        loadClassMap.put("int", int.class);
        loadClassMap.put("short", short.class);
        loadClassMap.put("long", long.class);
        loadClassMap.put("float", float.class);
        loadClassMap.put("double", double.class);
        loadClassMap.put("boolean", boolean.class);
        loadClassMap.put("char", char.class);
        loadClassMap.put("void", void.class);
    }

    protected static Class<?>[] JAXB_BUILTIN_CLASSES =
        {byte[].class, boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class,
         short.class, void.class, java.awt.Image.class, java.io.File.class, java.lang.Boolean.class,
         java.lang.Byte.class, java.lang.Character.class, java.lang.Class.class, java.lang.Double.class,
         java.lang.Float.class, java.lang.Integer.class, java.lang.Long.class, java.lang.Object.class,
         java.lang.Short.class, java.lang.String.class, java.lang.Void.class, java.math.BigDecimal.class,
         java.math.BigInteger.class, java.net.URI.class, java.net.URL.class, java.util.Calendar.class,
         java.util.Date.class, java.util.GregorianCalendar.class, java.util.UUID.class,
         javax.activation.DataHandler.class, javax.xml.bind.JAXBElement.class, javax.xml.datatype.Duration.class,
         javax.xml.datatype.XMLGregorianCalendar.class, javax.xml.namespace.QName.class,
         javax.xml.transform.Source.class};

    protected static final Set<Class<?>> BUILTIN_CLASSES_SET = new HashSet<Class<?>>(Arrays.asList(JAXB_BUILTIN_CLASSES));

    /*
    protected static Class<?>[] COMMON_ARRAY_CLASSES =
        new Class[] {char[].class, short[].class, int[].class, long[].class, float[].class, double[].class,
                     String[].class
                     };

    protected static final Set<Class<?>> COMMON_CLASSES_SET = new HashSet<Class<?>>(Arrays.asList(COMMON_ARRAY_CLASSES));
    */

    protected LRUCache<Object, JAXBContext> cache;
    protected Pool<JAXBContext, Marshaller>  mpool;
    protected Pool<JAXBContext, Unmarshaller> upool;

    // protected JAXBContext commonContext;
    protected JAXBContext defaultContext;
    private ExtensionPointRegistry registry;
    
    public JAXBContextCache(ExtensionPointRegistry registry) {
        this(CACHE_SIZE, CACHE_SIZE, CACHE_SIZE, registry);
    }

    public JAXBContextCache(int contextSize, int marshallerSize, int unmarshallerSize, ExtensionPointRegistry registry) {
        this.registry = registry;
        cache = new LRUCache<Object, JAXBContext>(contextSize);
        mpool = new Pool<JAXBContext, Marshaller>();
        upool = new Pool<JAXBContext, Unmarshaller>();
        defaultContext = getDefaultJAXBContext();
    }
    
    private JAXBContext newJAXBContext(final Class<?>... classesToBeBound) throws JAXBException {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<JAXBContext>() {
                public JAXBContext run() throws JAXBException {
                    // Try to set up TCCL so that JAXBContext service discovery works in OSGi
                    ClassLoader tccl =
                        ClassLoaderContext.setContextClassLoader(JAXBContextCache.class.getClassLoader(),
                                                                 registry.getServiceDiscovery(),
                                                                 // The service provider of JAXBContext doesn't extend JAXBContext
                                                                 // We should use the service name instead of the class
                                                                 JAXBContext.class.getName(), 
                                                                 DatatypeFactory.class.getName());
                    try {
                        JAXBContext context = JAXBContext.newInstance(classesToBeBound);
                        return context;
                    } finally {
                        if (tccl != null) {
                            Thread.currentThread().setContextClassLoader(tccl);
                        }
                    }
                }
            });
        } catch (PrivilegedActionException e) {
            throw (JAXBException)e.getException();
        }
    }


    public JAXBContext getDefaultJAXBContext() {
        try {
            return newJAXBContext();
        } catch (JAXBException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * @param name of primitive type
     * @return primitive Class or null
     */
    public static Class<?> getPrimitiveClass(String text) {
        return loadClassMap.get(text);
    }

    /**
     * Return the class for this name
     *
     * @return Class
     */
    private static Class<?> forName(final String className, final boolean initialize, final ClassLoader classloader)
        throws ClassNotFoundException {
        // NOTE: This method must remain private because it uses AccessController
        Class<?> cl = null;
        try {
            cl = AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
                public Class<?> run() throws ClassNotFoundException {
                    // Class.forName does not support primitives
                    Class<?> cls = getPrimitiveClass(className);
                    if (cls == null) {
                        cls = Class.forName(className, initialize, classloader);
                    }
                    return cls;
                }
            });
        } catch (PrivilegedActionException e) {
            throw (ClassNotFoundException)e.getException();
        }

        return cl;
    }

    public Marshaller getMarshaller(JAXBContext context) throws JAXBException {
        Marshaller marshaller = mpool.get(context);
        if (marshaller == null) {
            marshaller = context.createMarshaller();
        }
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        return marshaller;
    }

    public void releaseJAXBMarshaller(JAXBContext context, Marshaller marshaller) {
        if (marshaller != null) {
            marshaller.setAttachmentMarshaller(null);
            mpool.put(context, marshaller);
            // No point unsetting marshaller's JAXB_FRAGMENT property, since we'll just reset it when
            // doing the next get.
        }
    }

    public Unmarshaller getUnmarshaller(JAXBContext context) throws JAXBException {
        Unmarshaller unmarshaller = upool.get(context);
        if (unmarshaller == null) {
            unmarshaller = context.createUnmarshaller();
        }
        return unmarshaller;
    }

    public void releaseJAXBUnmarshaller(JAXBContext context, Unmarshaller unmarshaller) {
        if (unmarshaller != null) {
            unmarshaller.setAttachmentUnmarshaller(null);
            upool.put(context, unmarshaller);
        }
    }

    public LRUCache<Object, JAXBContext> getCache() {
        return cache;
    }

    public JAXBContext getJAXBContext(Class<?> cls) throws JAXBException {
        if (BUILTIN_CLASSES_SET.contains(cls)) {
            return defaultContext;
        }
        return getJAXBContext(new Class<?>[] {cls});
    }

    public JAXBContext getJAXBContext(Class<?>[] classes) throws JAXBException {
        Set<Class<?>> classSet = new HashSet<Class<?>>(Arrays.asList(classes));
        return getJAXBContext(classSet);
    }

    public JAXBContext getJAXBContext(Set<Class<?>> classes) throws JAXBException {
        // Remove the JAXB built-in types to maximize the cache hit
        Set<Class<?>> classSet = new HashSet<Class<?>>(classes);
        classSet.removeAll(BUILTIN_CLASSES_SET);

        // FIXME: [rfeng] Remove java classes that are mapped to the same XSD type to avoid
        // conflicts
        if (classSet.contains(Date[].class)) {
            classSet.remove(Calendar[].class);
        }

        if (classSet.contains(URI[].class)) {
            classSet.remove(UUID[].class);
        }

        if (classSet.contains(Source[].class)) {
            classSet.remove(Image[].class);
            classSet.remove(DataHandler[].class);
        }

        classSet = getJAXBClasses(classSet);

        if(classSet.isEmpty()) {
            return defaultContext;
        }

        synchronized (cache) {
            JAXBContext context = cache.get(classSet);
            if (context != null) {
                return context;
            }
            context = newJAXBContext(classSet.toArray(new Class<?>[classSet.size()]));
            cache.put(classSet, context);
            return context;
        }
    }

    public void clear() {
        synchronized (cache) {
            cache.clear();
        }
        /*
        synchronized (upool) {
            upool.clear();
        }
        synchronized (upool) {
            upool.clear();
        }
        */
    }

    //
    // This inner class is copied in its entirety from the Axis2 utility class,
    // org.apache.axis2.jaxws.message.databinding.JAXBUtils.   We could look into extending but it's such a basic data structure
    // without other dependencies so we might be better off copying it and avoiding a new
    // Axis2 dependency here.
    //

    /**
     * Pool a list of items for a specific key
     *
     * @param <K> Key
     * @param <V> Pooled object
     */
    private static class Pool<K,V> {
        private SoftReference<Map<K,List<V>>> softMap =
            new SoftReference<Map<K,List<V>>>(
                    new ConcurrentHashMap<K, List<V>>());

        // The maps are freed up when a LOAD FACTOR is hit
        private static final int MAX_LIST_FACTOR = 50;
        private static final int MAX_LOAD_FACTOR = 32;  // Maximum number of JAXBContext to store

        /**
         * @param key
         * @return removed item from pool or null.
         */
        public V get(K key) {
            List<V> values = getValues(key);
            synchronized (values) {
                if (values.size()>0) {
                    V v = values.remove(values.size()-1);
                    return v;

                }
            }
            return null;
        }

        /**
         * Add item back to pool
         * @param key
         * @param value
         */
        public void put(K key, V value) {
            adjustSize();
            List<V> values = getValues(key);
            synchronized (values) {
                if (values.size() < MAX_LIST_FACTOR) {
                    values.add(value);
                }
            }
        }

        /**
         * Get or create a list of the values for the key
         * @param key
         * @return list of values.
         */
        private List<V> getValues(K key) {
            Map<K,List<V>> map = softMap.get();
            List<V> values = null;
            if (map != null) {
                values = map.get(key);
                if(values !=null) {
                    return values;
                }
            }
            synchronized (this) {
                if (map != null) {
                    values = map.get(key);
                }
                if (values == null) {
                    if (map == null) {
                        map = new ConcurrentHashMap<K, List<V>>();
                        softMap =
                            new SoftReference<Map<K,List<V>>>(map);
                    }
                    values = new ArrayList<V>();
                    map.put(key, values);

                }
                return values;
            }
        }

        /**
         * AdjustSize
         * When the number of keys exceeds the maximum load, half
         * of the entries are deleted.
         *
         * The assumption is that the JAXBContexts, UnMarshallers, Marshallers, etc. require
         * a large footprint.
         */
        private void adjustSize() {
            Map<K,List<V>> map = softMap.get();
            if (map != null && map.size() > MAX_LOAD_FACTOR) {
                // Remove every other Entry in the map.
                Iterator it = map.entrySet().iterator();
                boolean removeIt = false;
                while (it.hasNext()) {
                    it.next();
                    if (removeIt) {
                        it.remove();
                    }
                    removeIt = !removeIt;
                }
            }
        }
        public void removeCtx(K key){
            Map<K,List<V>> map = softMap.get();
            if (map !=null && key !=null){
                map.remove(key);
            }
        }
    }

    /**
     * Find the JAXB classes (looking into packages) to be bound
     * @param classes A collection of classes
     * @return A set of classes that include the ObjectFactory and indexed JAXB classes
     * @throws JAXBException
     */
    private static Set<Class<?>> getJAXBClasses(Collection<Class<?>> classes) throws JAXBException {
        Set<Class<?>> classSet = new HashSet<Class<?>>();
        // Index the packages
        Map<Package, ClassLoader> pkgs = getPackages(classes);
        Set<Package> nonJAXBPackages = new HashSet<Package>();
        for (Map.Entry<Package, ClassLoader> p : pkgs.entrySet()) {
            Package pkg = p.getKey();
            if (pkg == null) {
                continue;
            }
            Set<Class<?>> set = getJAXBClasses(pkg.getName(), p.getValue());
            if (set.isEmpty()) {
                // No JAXB package
                nonJAXBPackages.add(pkg);
            } else {
                // Add JAXB ObjectFactory and indexed classes
                classSet.addAll(set);
            }
        }
        // Adding classes that are not part of JAXB packages
        for (Class<?> cls : classes) {

            Package pkg = getPackage(cls);
            if (pkg == null || nonJAXBPackages.contains(pkg)) {
                classSet.add(cls);
            } else {
                // TUSCANY-3162: Test if a class is generated by JAXB
                // There might be the case that non-JAXB classes are in the same package as the JAXB classes
                if (!cls.isAnnotationPresent(XmlType.class) 
                    && !cls.isAnnotationPresent(XmlEnum.class)
                    && !cls.isAnnotationPresent(XmlSeeAlso.class)
                    && !cls.isAnnotationPresent(XmlRootElement.class)
                    && !cls.isAnnotationPresent(XmlTransient.class)) {
                    classSet.add(cls);
                }
            }
        }
        return classSet;
    }

    /**
     * Get the package for a class, taking array into account
     * @param cls
     * @return
     */
    private static Package getPackage(Class<?> cls) {
        Class<?> type = cls;
        while (type.isArray()) {
            type = type.getComponentType();
        }
        return type.getPackage();
    }

    /**
     * Get a map of packages
     * @param classes
     * @return
     */
    private static Map<Package, ClassLoader> getPackages(Collection<Class<?>> classes) {
        Map<Package, ClassLoader> pkgs = new HashMap<Package, ClassLoader>();
        for (Class<?> cls : classes) {
            Package pkg = getPackage(cls);
            if (pkg != null) {
                final Class fcls = cls;
                ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
                    ClassLoader cl = fcls.getClassLoader();
                    return cl;
                 }
            });
                pkgs.put(pkg, cl);
            }
        }
        return pkgs;
    }

    /**
     * Find ObjectFactory and indexed JAXB classes for the package
     * @param pkg
     * @param classLoader
     * @return
     * @throws JAXBException
     */
    private static Set<Class<?>> getJAXBClasses(String pkg, ClassLoader classLoader) throws JAXBException {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        List<Class<?>> indexedClasses;

        // look for ObjectFactory and load it
        final Class<?> o;
        try {
            o = forName(pkg + ".ObjectFactory", false, classLoader);
            classes.add(o);
        } catch (ClassNotFoundException e) {
            // not necessarily an error
        }

        // look for jaxb.index and load the list of classes
        try {
            indexedClasses = loadIndexedClasses(pkg, classLoader);
        } catch (IOException e) {
            throw new JAXBException(e);
        }
        if (indexedClasses != null) {
            classes.addAll(indexedClasses);
        }

        return classes;
    }

    /**
     * Look for jaxb.index file in the specified package and load it's contents
     *
     * @param pkg package name to search in
     * @param classLoader ClassLoader to search in
     * @return a List of Class objects to load, null if there weren't any
     * @throws IOException if there is an error reading the index file
     * @throws JAXBException if there are any errors in the index file
     */
    private static List<Class<?>> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException,
        JAXBException {
        if (classLoader == null) {
            return null;
        }
        final String resource = pkg.replace('.', '/') + "/jaxb.index";
        final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);

        if (resourceAsStream == null) {
            return null;
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8"));
        try {
            List<Class<?>> classes = new ArrayList<Class<?>>();
            String className = in.readLine();
            while (className != null) {
                className = className.trim();
                if (className.startsWith("#") || (className.length() == 0)) {
                    className = in.readLine();
                    continue;
                }

                try {
                    classes.add(forName(pkg + '.' + className, false, classLoader));
                } catch (ClassNotFoundException e) {
                    throw new JAXBException(e);
                }

                className = in.readLine();
            }
            return classes;
        } finally {
            in.close();
        }
    }

    public void removeJAXBContextFromPools(JAXBContext ctx){
        if (mpool != null && ctx != null){
            mpool.removeCtx(ctx);
        }
        if (upool != null && ctx !=null){
            upool.removeCtx(ctx);
        }
    }
    
    /**
     * Removes all the cached information relating to a contribution. The 
     * contribution is identified by the contribution classloader passed in 
     * as a parameter. This is used when a contribution is removed from 
     * the runtime. 
     * 
     * @param contributionClassloader
     */
    public void removeJAXBContextForContribution(ClassLoader contributionClassloader){
        if (cache != null){
            try {
                synchronized(cache) {
                    Set<Object> objSet = cache.keySet();
                    List<Object> toRemove = new ArrayList<Object>();
                    Iterator<Object> i = objSet.iterator();
                    while(i.hasNext()) {
                        Object obj = i.next();
                        if (obj instanceof Set){
                            Set<Class> innerSet = (Set<Class>)obj;
                            Iterator<Class> j = innerSet.iterator();
                            loop:
                            while(j.hasNext()) {
                                Class cls = j.next();
                                ClassLoader cl = cls.getClassLoader();
                                while (cl != null){
                                    if (cl == contributionClassloader){
                                        toRemove.add(obj);
                                        break loop;
                                    }
                                    // take account of generated classes
                                    cl = cl.getParent();
                                }
                            }
                        } 
                    }
                    for (Object obj : toRemove){
                        JAXBContext ctx = cache.get(obj);
                        removeJAXBContextFromPools(ctx);
                        cache.remove(obj);
                    }
                }
            } catch(Exception e) {
                throw new ServiceRuntimeException(e);
            }
        }
    }


}