KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > util > PeerFactory


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.util;
31
32 import java.io.IOException JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import nextapp.echo2.app.util.PropertiesDiscovery;
38
39 /**
40  * A mechanism for retrieving instances of singleton peer objects which are
41  * defined to each support a specific <code>Class</code>.
42  * A properties file is used to associate peer classes with their supported
43  * classes. The properties file should contain the fully qualified class
44  * names of the supported objects as its keys. The values of the properties
45  * file should contain the fully qualified class names of the peer objects.
46  * A single instance of each peer class will be used to support ALL instances
47  * of the supported class.
48  */

49 public class PeerFactory {
50     
51     private final Map JavaDoc objectClassNameToPeerMap = new HashMap JavaDoc();
52     
53     /**
54      * Creates a new <code>PeerFactory</code>.
55      *
56      * @param resourceName the name of the resource properties file from which
57      * the peer bindings may be retrieved (this file will be retrieved
58      * using the <code>PropertiesDiscovery</code> system, so multiple
59      * instances of the file within the <code>CLASSPATH</code> will be
60      * automatically discovered.
61      * @param classLoader the <code>ClassLoader</code> to use for retrieving the
62      * resource file and for instantiating the peer singleton instances
63      */

64     public PeerFactory(String JavaDoc resourceName, ClassLoader JavaDoc classLoader) {
65         try {
66             Map JavaDoc peerNameMap = PropertiesDiscovery.loadProperties(resourceName, classLoader);
67             Iterator JavaDoc it = peerNameMap.keySet().iterator();
68             while (it.hasNext()) {
69                 String JavaDoc objectClassName = (String JavaDoc) it.next();
70                 String JavaDoc peerClassName = (String JavaDoc) peerNameMap.get(objectClassName);
71                 Class JavaDoc peerClass = classLoader.loadClass(peerClassName);
72                 Object JavaDoc peer = peerClass.newInstance();
73                 objectClassNameToPeerMap.put(objectClassName, peer);
74             }
75         } catch (ClassNotFoundException JavaDoc ex) {
76             throw new RuntimeException JavaDoc("Unable to load synchronize peer bindings: " + ex);
77         } catch (IOException JavaDoc ex) {
78             throw new RuntimeException JavaDoc("Unable to load synchronize peer bindings: " + ex);
79         } catch (InstantiationException JavaDoc ex) {
80             throw new RuntimeException JavaDoc("Unable to load synchronize peer bindings: " + ex);
81         } catch (IllegalAccessException JavaDoc ex) {
82             throw new RuntimeException JavaDoc("Unable to load synchronize peer bindings: " + ex);
83         }
84     }
85     
86     /**
87      * Retrieves the appropriate peer instance for a given object
88      * <code>Class</code>. Returns null in the event that no peer is provided
89      * to support the specified class.
90      *
91      * @param objectClass the supported object class
92      * @param searchSuperClasses flag indicating whether superclasses
93      * of <code>objectClass</code> should be searched for peers if
94      * none can be found for <code>objectClass</code> itself
95      * @return the relevant peer, or null if none can be found
96      */

97     public Object JavaDoc getPeerForObject(Class JavaDoc objectClass, boolean searchSuperClasses) {
98         Object JavaDoc peer = null;
99         do {
100             peer = objectClassNameToPeerMap.get(objectClass.getName());
101             if (peer != null) {
102                 return peer;
103             }
104             objectClass = objectClass.getSuperclass();
105         } while (searchSuperClasses && objectClass != null);
106         return null;
107     }
108 }
109
Popular Tags