KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > hessian > HessianConfig


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.remote.hessian;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.map.EntityResolver;
27 import org.apache.cayenne.util.Util;
28
29 import com.caucho.hessian.io.AbstractSerializerFactory;
30 import com.caucho.hessian.io.SerializerFactory;
31
32 /**
33  * A utility class that configures Hessian serialization properties using reflection.
34  *
35  * @since 1.2
36  * @author Andrus Adamchik
37  */

38 public class HessianConfig {
39
40     /**
41      * Creates a Hessian SerializerFactory configured with zero or more
42      * AbstractSerializerFactory extensions. Extensions are specified as class names. This
43      * method can inject EntityResolver if an extension factory class defines
44      * <em>setEntityResolver(EntityResolver)</em> method.
45      *
46      * @param factoryNames an array of factory class names. Each class must be a concrete
47      * subclass of <em>com.caucho.hessian.io.AbstractSerializerFactory</em>
48      * and have a default constructor.
49      * @param resolver if not null, EntityResolver will be injected into all factories
50      * that implement <em>setEntityResolver(EntityResolver)</em> method.
51      */

52     public static SerializerFactory createFactory(
53             String JavaDoc[] factoryNames,
54             EntityResolver resolver) {
55
56         SerializerFactory factory = new SerializerFactory();
57
58         if (factoryNames != null && factoryNames.length > 0) {
59
60             for (int i = 0; i < factoryNames.length; i++) {
61
62                 try {
63                     factory.addFactory(loadFactory(factoryNames[i], resolver));
64                 }
65                 catch (Exception JavaDoc e) {
66                     throw new CayenneRuntimeException("Error configuring factory class "
67                             + factoryNames[i], e);
68                 }
69             }
70         }
71
72         return factory;
73     }
74
75     static AbstractSerializerFactory loadFactory(
76             String JavaDoc factoryName,
77             EntityResolver resolver) throws Exception JavaDoc {
78
79         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
80         Class JavaDoc factoryClass = Class.forName(factoryName, true, loader);
81
82         if (!AbstractSerializerFactory.class.isAssignableFrom(factoryClass)) {
83             throw new IllegalArgumentException JavaDoc(factoryClass
84                     + " is not a AbstractSerializerFactory");
85         }
86
87         Constructor JavaDoc c = factoryClass.getDeclaredConstructor(new Class JavaDoc[] {});
88         if (!Util.isAccessible(c)) {
89             c.setAccessible(true);
90         }
91
92         AbstractSerializerFactory object = (AbstractSerializerFactory) c
93                 .newInstance(null);
94
95         if (resolver != null) {
96             try {
97
98                 Method JavaDoc setter = factoryClass.getDeclaredMethod(
99                         "setEntityResolver",
100                         new Class JavaDoc[] {
101                             EntityResolver.class
102                         });
103
104                 if (!Util.isAccessible(setter)) {
105                     setter.setAccessible(true);
106                 }
107
108                 setter.invoke(object, new Object JavaDoc[] {
109                     resolver
110                 });
111             }
112             catch (Exception JavaDoc e) {
113                 // ignore injection exception
114
}
115         }
116
117         return object;
118     }
119
120 }
121
Popular Tags