2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.marshal;
25
import org.jboss.logging.Logger;
26
import org.jboss.remoting.InvokerLocator;
27
import org.jboss.remoting.marshal.http.HTTPMarshaller;
28
import org.jboss.remoting.marshal.http.HTTPUnMarshaller;
29
import org.jboss.remoting.marshal.rmi.RMIMarshaller;
30
import org.jboss.remoting.marshal.rmi.RMIUnMarshaller;
31
import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
32
import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller;
34
import java.util.Collections;
35
import java.util.HashMap;
40
* This class will provide marshallers and unmarshallers for data based on
41
* the data type want to marshal to. The most common will be just to serialize
42
* the data. However, may have jaxrpc, IIOP, serializers. Can also have marshallers
43
* and unmarshallers based on class type. For example, might be marshaller/unmarshaller
44
* for the Transaction class.
46
* @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
48
public class MarshalFactory
50
private static Map marshallers = Collections.synchronizedMap(new HashMap());
51
private static Map unmarshallers = Collections.synchronizedMap(new HashMap());
52
private static Map classMarshallers = Collections.synchronizedMap(new HashMap());
53
private static Map classUnmarshallers = Collections.synchronizedMap(new HashMap());
55
protected final static Logger log = Logger.getLogger(MarshalFactory.class);
57
private final static boolean isTrace = log.isTraceEnabled();
58
private final static boolean isDebug = log.isDebugEnabled();
60
// statically load core marshallers/unmarshallers
65
marshallers.put(SerializableMarshaller.DATATYPE, new SerializableMarshaller());
66
unmarshallers.put(SerializableUnMarshaller.DATATYPE, new SerializableUnMarshaller());
67
marshallers.put(HTTPMarshaller.DATATYPE, new HTTPMarshaller());
68
unmarshallers.put(HTTPUnMarshaller.DATATYPE, new HTTPUnMarshaller());
69
marshallers.put(RMIMarshaller.DATATYPE, new RMIMarshaller());
70
unmarshallers.put(RMIUnMarshaller.DATATYPE, new RMIUnMarshaller());
74
log.error("Could not statically load default marshallers.", e);
79
* Will add the marshaller and unmarshaller based on class type. Each can then be retrieved using the
86
public static void addMarshaller(Class classType, Marshaller marshaller, UnMarshaller unMarshaller)
88
classMarshallers.put(classType, marshaller);
89
classUnmarshallers.put(classType, unMarshaller);
93
* Adds the marshaller and unmarshaller based on data type. Each can then be retrieved using the data type
100
public static void addMarshaller(String dataType, Marshaller marshaller, UnMarshaller unMarshaller)
102
marshallers.put(dataType, marshaller);
103
unmarshallers.put(dataType, unMarshaller);
107
* Looks up marshaller by class type. Will return null if not found.
112
public static Marshaller getMarshaller(Class classType)
114
Marshaller marshaller = null;
115
Object obj = classMarshallers.get(classType);
116
if(obj != null && obj instanceof Marshaller)
118
marshaller = (Marshaller) obj;
122
marshaller = marshaller.cloneMarshaller();
124
catch(CloneNotSupportedException e)
126
log.warn("Could not clone " + marshaller);
133
log.trace("Could not find marshaller for class type '" + classType + "'. Object in collection is " + obj);
141
* Looks up marshaller by class type. Will return null if not found.
146
public static Marshaller getMarshaller(Class classType, String serializationType)
148
Marshaller marshaller = getMarshaller(classType);
149
if(marshaller instanceof SerialMarshaller)
151
((SerialMarshaller) marshaller).setSerializationType(serializationType);
157
* Returns unmarshaller by class type. Will return null if not found.
162
public static UnMarshaller getUnMarshaller(Class classType)
164
UnMarshaller unmarshaller = null;
165
Object obj = classUnmarshallers.get(classType);
166
if(obj != null && obj instanceof UnMarshaller)
168
unmarshaller = (UnMarshaller) obj;
172
unmarshaller = unmarshaller.cloneUnMarshaller();
174
catch(CloneNotSupportedException e)
176
log.warn("Could not clone " + unmarshaller);
184
log.trace("Could not find unmarshaller for class type '" + classType + "'. Object in collection is " + obj);
190
public static UnMarshaller getUnMarshaller(Class classType, String serializationType)
192
UnMarshaller unmarshaller = getUnMarshaller(classType);
193
if(unmarshaller instanceof SerializableUnMarshaller)
195
((SerializableUnMarshaller) unmarshaller).setSerializationType(serializationType);
202
* Gets marshaller based on data type (i.e. serializable) and based on the marshallers registered with the factory.
205
* @return The marshaller or null if none for for the specified type
207
public static Marshaller getMarshaller(String dataType)
209
Marshaller marshaller = null;
210
Object obj = marshallers.get(dataType);
211
if(obj != null && obj instanceof Marshaller)
213
marshaller = (Marshaller) obj;
217
marshaller = marshaller.cloneMarshaller();
219
catch(CloneNotSupportedException e)
221
log.warn("Could not clone " + marshaller);
229
log.trace("Could not find marshaller for data type '" + dataType + "'. Object in collection is " + obj);
237
public static Marshaller getMarshaller(String dataType, String serializationType)
239
Marshaller marshaller = getMarshaller(dataType);
240
if(marshaller instanceof SerializableMarshaller)
242
((SerializableMarshaller) marshaller).setSerializationType(serializationType);
248
public static UnMarshaller getUnMarshaller(String dataType, String serializationType)
250
UnMarshaller unmarshaller = getUnMarshaller(dataType);
251
if(unmarshaller instanceof SerializableUnMarshaller)
253
((SerializableUnMarshaller) unmarshaller).setSerializationType(serializationType);
261
* Gets the marshaller based on data type (i.e. serialziable) and based on the unmarshallers registered with the factory.
264
* @return The unmarshaller or null if none for the specified type
266
public static UnMarshaller getUnMarshaller(String dataType)
268
UnMarshaller unmarshaller = null;
269
Object obj = unmarshallers.get(dataType);
270
if(obj != null && obj instanceof UnMarshaller)
272
unmarshaller = (UnMarshaller) obj;
276
unmarshaller = unmarshaller.cloneUnMarshaller();
278
catch(CloneNotSupportedException e)
280
log.warn("Could not clone " + unmarshaller);
287
log.trace("Could not find unmarshaller for data type '" + dataType + "'. Object in collection is " + obj);
295
* Will try to look up marshaller by first looking for data type parameter within locator and then using that
296
* to look up marhsaller locally. If can not find it, will then look to see if can find the 'marshaller' parameter
297
* within the locator parameters. If found, will try to load the marshaller by the class name specified as the parameter
298
* value. If still can not find the class within the local VM, will look to see if there is a parameter for
299
* the server's marshaller loader port. If this exists, will then try calling on the remote server to load the
300
* marshaller (and its related classes) within the local VM. If still can not be found, will return null.
306
public static Marshaller getMarshaller(InvokerLocator locator, ClassLoader classLoader)
308
return getMarshaller(locator, classLoader, null);
312
* Will try to look up marshaller by first looking for data type parameter within locator and config and then using that
313
* to look up marhsaller locally. If can not find it, will then look to see if can find the 'marshaller' parameter
314
* within the locator parameters or config parameters. If found, will try to load the marshaller by the class name specified
315
* as the parameter value. If still can not find the class within the local VM, will look to see if there is a parameter for
316
* the server's marshaller loader port. If this exists, will then try calling on the remote server to load the
317
* marshaller (and its related classes) within the local VM. If still can not be found, will return null.
324
public static Marshaller getMarshaller(InvokerLocator locator, ClassLoader classLoader, Map config)
326
String serializationType = locator.findSerializationType();
327
Marshaller marshaller = null;
328
if(locator != null || config != null)
330
Map params = new HashMap();
331
if (locator.getParameters() != null)
333
params.putAll(locator.getParameters());
337
params.putAll(config);
341
// start with data type as is prefered method of getting marshaller/unmarshaller
342
String dataType = (String) params.get(InvokerLocator.DATATYPE);
345
dataType = (String) params.get(InvokerLocator.DATATYPE_CASED);
349
marshaller = getMarshaller(dataType);
351
if(marshaller == null)
355
log.trace("Could not look up marshaller by data type ('" + dataType + "'). Will try to load dynamically.");
358
// will now look for explicit param for marshaller class
359
String marshallerFQN = (String) params.get(InvokerLocator.MARSHALLER);
360
marshaller = loadMarshaller(marshallerFQN);
361
if(marshaller != null)
365
log.trace("Found marshaller by loading locally.");
367
// try to load unmarshaller so that can add to list
368
String unmarshallerFQN = (String) params.get(InvokerLocator.UNMARSHALLER);
369
UnMarshaller unmarshaller = loadUnMarshaller(unmarshallerFQN);
370
if(unmarshaller != null)
372
addMarshaller(dataType, marshaller, unmarshaller);
376
if(marshaller == null && isTrace)
378
log.trace("Tried to find marshaller from locator by both data type and class name but was unsuccessful. " +
379
"Will try to load it from remote server.");
381
// if still have not found marshaller, check to see if can load remotely
382
if(marshaller == null && dataType != null)
384
InvokerLocator loaderLocator = MarshallLoaderFactory.convertLocator(locator);
385
if(loaderLocator != null)
387
marshaller = MarshallerLoaderClient.getMarshaller(loaderLocator, dataType, classLoader);
388
UnMarshaller unmarshaller = MarshallerLoaderClient.getUnMarshaller(loaderLocator, dataType, classLoader);
389
if(unmarshaller != null)
391
unmarshaller.setClassLoader(classLoader);
395
log.debug("Remotely loaded marshaller: " + marshaller);
396
log.debug("Remotely loaded unmarshaller: " + unmarshaller);
398
if(marshaller != null && unmarshaller != null)
400
addMarshaller(dataType, marshaller, unmarshaller);
407
if(marshaller != null)
411
marshaller = marshaller.cloneMarshaller();
413
catch(CloneNotSupportedException e)
415
log.warn("Could not clone " + marshaller);
419
if(marshaller instanceof SerialMarshaller)
421
((SerialMarshaller) marshaller).setSerializationType(serializationType);
426
private static Marshaller loadMarshaller(String marshallerFQN)
428
Marshaller marshaller = null;
429
if(marshallerFQN != null)
433
Class marshallerClass = Class.forName(marshallerFQN);
434
marshaller = (Marshaller) marshallerClass.newInstance();
438
log.warn("Found marshaller fully qualified class name within locator parameters, but was unable " +
439
"to load class: " + marshallerFQN);
446
* Will try to look up unmarshaller by first looking for data type parameter within locator and then using that
447
* to look up unmarshaller locally. If can not find it, will then look to see if can find the 'unmarshaller' parameter
448
* within the locator parameters. If found, will try to load the unmarshaller by the class name specified as the parameter
449
* value. If still can not find the class within the local VM, will look to see if there is a parameter for
450
* the server's marshaller loader port. If this exists, will then try calling on the remote server to load the
451
* unmarshaller (and its related classes) within the local VM. If still can not be found, will return null.
457
public static UnMarshaller getUnMarshaller(InvokerLocator locator, ClassLoader classLoader)
459
return getUnMarshaller(locator, classLoader, null);
463
* Will try to look up unmarshaller by first looking for data type parameter within locator and config map and then using that
464
* to look up unmarshaller locally. If can not find it, will then look to see if can find the 'unmarshaller' parameter
465
* within the locator parameters or config map. If found, will try to load the unmarshaller by the class name specified as the
466
* parameter value. If still can not find the class within the local VM, will look to see if there is a parameter for
467
* the server's marshaller loader port. If this exists, will then try calling on the remote server to load the
468
* unmarshaller (and its related classes) within the local VM. If still can not be found, will return null.
475
public static UnMarshaller getUnMarshaller(InvokerLocator locator, ClassLoader classLoader, Map config)
477
String serializationType = locator.findSerializationType();
478
UnMarshaller unmarshaller = null;
479
if(locator != null || config != null)
481
Map params = new HashMap();
482
if (locator.getParameters() != null)
484
params.putAll(locator.getParameters());
488
params.putAll(config);
492
// start with data type as is prefered method of getting marshaller/unmarshaller
493
String dataType = (String) params.get(InvokerLocator.DATATYPE);
496
dataType = (String) params.get(InvokerLocator.DATATYPE_CASED);
500
unmarshaller = getUnMarshaller(dataType);
502
if(unmarshaller == null)
506
log.trace("Could not find unmarshaller by data type ('" + dataType + "'). Will try to load dynamically.");
509
// will now look for explicit param for marshaller class
510
String unmarshallerFQN = (String) params.get(InvokerLocator.UNMARSHALLER);
511
unmarshaller = loadUnMarshaller(unmarshallerFQN);
512
if(unmarshaller != null)
514
String marshallerFQN = (String) params.get(InvokerLocator.MARSHALLER);
515
Marshaller marshaller = loadMarshaller(marshallerFQN);
516
if(marshaller != null)
518
addMarshaller(dataType, marshaller, unmarshaller);
522
if(isTrace && unmarshaller == null)
524
log.trace("Tried to find unmarshaller from locator by both data type and class name but was unsuccessful.");
526
// if still have not found unmarshaller, check to see if can load remotely
527
if(unmarshaller == null && dataType != null)
529
InvokerLocator loaderLocator = MarshallLoaderFactory.convertLocator(locator);
530
unmarshaller = MarshallerLoaderClient.getUnMarshaller(loaderLocator, dataType, classLoader);
531
if(unmarshaller != null)
533
unmarshaller.setClassLoader(classLoader);
535
Marshaller marshaller = MarshallerLoaderClient.getMarshaller(loaderLocator, dataType, classLoader);
538
log.trace("Remotely loaded marshaller: " + marshaller);
539
log.trace("Remotely loaded unmarshaller: " + unmarshaller);
541
if(marshaller != null && unmarshaller != null)
543
addMarshaller(dataType, marshaller, unmarshaller);
549
if(unmarshaller != null)
553
unmarshaller = unmarshaller.cloneUnMarshaller();
555
catch(CloneNotSupportedException e)
557
log.warn("Could not clone " + unmarshaller);
560
if(unmarshaller instanceof SerializableUnMarshaller)
562
((SerializableUnMarshaller) unmarshaller).setSerializationType(serializationType);
567
private static UnMarshaller loadUnMarshaller(String unmarshallerFQN)
569
UnMarshaller unmarshaller = null;
570
if(unmarshallerFQN != null)
574
Class unmarshallerClass = Class.forName(unmarshallerFQN);
575
unmarshaller = (UnMarshaller) unmarshallerClass.newInstance();
579
log.error("Found unmarshaller fully qualified class name within locator parameters, but was unable " +
580
"to load class: " + unmarshallerFQN, e);
b'\\ No newline at end of file'