1   /*
2    * (C) 2002 David Carr  david@carr.name
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation; either version 2 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  
19  package net.sourceforge.mflow.impl;
20  
21  import java.text.ParseException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.TooManyListenersException;
25  import java.util.logging.Logger;
26  
27  import net.sourceforge.mflow.api.ContactMethod;
28  import net.sourceforge.mflow.api.ContactMethodManager;
29  import net.sourceforge.mflow.api.PluginManager;
30  import net.sourceforge.mflow.reflect.ParamArgPair;
31  import net.sourceforge.mflow.reflect.ReflectionException;
32  import net.sourceforge.mflow.reflect.ReflectionUtil;
33  
34  /***
35   * A class for managing instances of ContactMethod
36   * 
37   * @author <a href="mailto:david@carr.name">David Carr</a>
38   * @version $Revision: 1.3 $
39   */
40  public class ContactMethodManagerImpl implements ContactMethodManager {
41    private List instances = new ArrayList();
42  
43    /***
44     * Private storage for all ContactMethod implementations as Class objects
45     */
46    private List implementations = new ArrayList();
47  
48    /***
49     * Private storage for all protocols as Strings (parallel to
50     * this.implementations)
51     */
52    private List protocols = new ArrayList();
53  
54    private static Logger log =
55      Logger.getLogger(ContactMethodManagerImpl.class.getName());
56  
57    private PluginManager pluginManager;
58  
59    /***
60     * Default constructor. Makes a new ContactMethodManager.
61     * 
62     * @param pluginManager the plugin manager to use
63     */
64    ContactMethodManagerImpl(PluginManager pluginManager) {
65      log.entering("ContactMethodManager", "(PluginManager)");
66      this.pluginManager = pluginManager;
67      log.exiting("ContactMethodManager", "(PluginManager)");
68    }
69  
70    public synchronized void addContactMethodType(Class cmt, String protocol)
71      throws TooManyListenersException, ClassCastException {
72      if (cmt == null) {
73        log.warning(
74          "Called ContactMethodManager.addContactMethodType with null implementation");
75        return;
76      }
77      log.entering(
78        "ContactMethodManager",
79        "addContactMethodType(" + cmt.getName() + ", " + protocol + ")");
80      int index = this.protocols.indexOf(protocol);
81      if (index > -1) {
82        Class c = (Class) this.implementations.get(index);
83        throw new TooManyListenersException(
84          "Protocol " + protocol + " already defined by " + c.getName());
85      }
86      if (!Util.implementsInterface(cmt, ContactMethod.class)) {
87        throw new ClassCastException(
88          cmt.getName() + " does not implement ContactMethod");
89      }
90      log.info(
91        "ContactMethod definition added: "
92          + cmt.getName()
93          + " handles protocol "
94          + protocol);
95      this.implementations.add(cmt);
96      this.protocols.add(protocol);
97    }
98  
99    public ContactMethod parse(String s) throws ParseException {
100     log.entering("ContactMethodManager", "parse(" + s + ")");
101     int colon = s.indexOf(':');
102     if (colon == -1)
103       throw new ParseException(s, -1);
104     String pr = s.substring(0, colon);
105     log.fine("Protocol: " + pr);
106     String id = s.substring(colon + 1);
107     log.fine("Identifier: " + id);
108     return getInstance(pr, id);
109   }
110 
111   public synchronized ContactMethod getInstance(
112     String protocol,
113     String identifier) {
114     log.entering(
115       "ContactMethodManager",
116       "getInstance(" + protocol + ", " + identifier + ")");
117     for (int i = 0;
118       i < this.instances.size();
119       i++) { //check for an existing instance
120       ContactMethod cm = (ContactMethod) this.instances.get(i);
121       if (cm.getProtocol().equals(protocol)
122         && cm.getIdentifier().equals(identifier)) {
123         log.finer("Found existing instance of contact method");
124         log.exiting(
125           "ContactMethodManager",
126           "getInstance(" + protocol + ", " + identifier + ")",
127           cm);
128         return cm; //found one, return it
129       }
130     }
131     int index = this.protocols.indexOf(protocol);
132     if (index > -1) {
133       Class c = (Class) this.implementations.get(index);
134       log.fine(
135         "Found implementation " + c.getName() + " for protocol " + protocol);
136       ContactMethod cm = newInstanceOf(c);
137       log.info("Made new instance of " + c.getName());
138       cm.setIdentifier(identifier);
139       log.info("Set identifier to " + identifier);
140       this.instances.add(cm);
141       log.exiting(
142         "ContactMethodManager",
143         "getInstance(" + protocol + ", " + identifier + ")",
144         cm);
145       return cm;
146     } else {
147       log.fine("No implementation for target protocol " + protocol);
148       log.exiting(
149         "ContactMethodManager",
150         "getInstance(" + protocol + ", " + identifier + ")",
151         null);
152       return null;
153     }
154   }
155 
156   private ContactMethod newInstanceOf(Class c) {
157     List paramArgs = new ArrayList();
158     Class[] configArgs = { PluginManager.class };
159     Object[] configParams = { this.pluginManager };
160     paramArgs.add(new ParamArgPair(configArgs, configParams));
161     paramArgs.add(new ParamArgPair());
162     try {
163       return (ContactMethod) ReflectionUtil.instantiate(c, paramArgs);
164     } catch (ReflectionException re) {
165       log.throwing(getClass().getName(), "newInstanceOf", re);
166       return null;
167     }
168   }
169 
170   public synchronized String[] getProtocols() {
171     return (String[]) this.protocols.toArray(new String[this.protocols.size()]);
172   }
173 }
This page was automatically generated by Maven