001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.collab.channel.filesharing.mdc.util;
042:
043: import com.sun.collablet.CollabException;
044:
045: import org.openide.execution.*;
046:
047: import java.io.*;
048:
049: import java.lang.reflect.Method;
050:
051: import java.net.*;
052:
053: import java.util.*;
054:
055: import org.netbeans.modules.collab.channel.filesharing.mdc.configbean.*;
056: import org.netbeans.modules.collab.core.Debug;
057:
058: /**
059: * Default EventProcessor
060: *
061: * @author Ayub Khan, ayub.khan@sun.com
062: * @version 1.0
063: */
064: public class CollabNotifierConfig extends CollabConfigVerifier {
065: ////////////////////////////////////////////////////////////////////////////
066: // Instance variables
067: ////////////////////////////////////////////////////////////////////////////
068:
069: /* normalizedEventID Map */
070: protected HashMap normalizedEventIDMap = new HashMap();
071:
072: /**
073: * constructor
074: *
075: */
076: public CollabNotifierConfig() {
077: super ();
078: }
079:
080: /**
081: * constructor
082: *
083: */
084: public CollabNotifierConfig(String currentVersion) {
085: super (currentVersion);
086: }
087:
088: ////////////////////////////////////////////////////////////////////////////
089: // methods
090: ////////////////////////////////////////////////////////////////////////////
091:
092: /**
093: * init
094: *
095: */
096: protected void init(String configURL) throws CollabException {
097: loadConfig(configURL);
098: }
099:
100: /**
101: * getNormalizedEventID
102: *
103: * @return normalized event ID Map
104: */
105: public HashMap getNormalizedEventID() {
106: return this .normalizedEventIDMap;
107: }
108:
109: /**
110: * getNormalizedEventID
111: *
112: * @return normalized event ID for given event
113: */
114: public String getNormalizedEventID(String eventBeanID) {
115: return (String) this .normalizedEventIDMap.get(eventBeanID);
116: }
117:
118: /**
119: * load config for notifer
120: *
121: * @param configURL
122: */
123: protected void loadConfig(String configURL) throws CollabException {
124: if (configURL == null) {
125: throw new IllegalArgumentException("config URL null: ");
126: }
127:
128: CCollab collab = null;
129:
130: try {
131: URL url = new URL(configURL);
132: InputStream in = url.openStream();
133:
134: //create DOM
135: collab = CCollab.read(in);
136: } catch (java.net.MalformedURLException murlex) {
137: throw new CollabException(murlex);
138: } catch (javax.xml.parsers.ParserConfigurationException parsex) {
139: throw new CollabException(parsex);
140: } catch (org.xml.sax.SAXException saxx) {
141: throw new CollabException(saxx);
142: } catch (java.io.IOException iox) {
143: throw new CollabException(iox);
144: }
145:
146: if (collab == null) {
147: throw new IllegalArgumentException(
148: "config load failed for: " + configURL);
149: }
150:
151: //get config
152: Config[] config = collab.getMdcConfig();
153:
154: for (int i = 0; i < config.length; i++) {
155: //getconfigVersion
156: String configVersion = config[i].getVersion();
157:
158: //get NotifierConfig
159: EventNotifierConfig notifierConfig = config[i]
160: .getMdcEventNotifierConfig();
161:
162: //get all registered events
163: RegisterEvent[] registerEvents = notifierConfig
164: .getRegisterEvent();
165:
166: for (int j = 0; j < registerEvents.length; j++) {
167: RegisterEvent registerEvent = registerEvents[j];
168: String eventName = registerEvent.getEventName();
169: String normalizedEventID = createUniqueNormalizedEventID(
170: configVersion, eventName);
171: String eventClassName = registerEvent.getEventClass();
172: Debug.log(this , "eventClassName: " + eventClassName); //NoI18n
173:
174: try {
175: ClassLoader cl = new NbClassLoader();
176: Class myClass = Class.forName(eventClassName, true,
177: cl);
178: Method getEventIDMethod = findGetEventID(myClass);
179: String eventBeanID = (String) getEventIDMethod
180: .invoke(null, new Object[] {});
181: Debug.log(this , "Strong type eventID: "
182: + eventBeanID); //NoI18n
183: Debug.log(this , "normalized eventID: "
184: + normalizedEventID); //NoI18n
185: normalizedEventIDMap.put(eventBeanID,
186: normalizedEventID);
187: } catch (ClassNotFoundException classNotFound) {
188: throw new CollabException(classNotFound);
189: } catch (java.lang.InstantiationException iex) {
190: throw new CollabException(iex);
191: } catch (java.lang.IllegalAccessException iax) {
192: throw new CollabException(iax);
193: } catch (ExceptionInInitializerError eiie) {
194: Throwable t = eiie.getException();
195:
196: if (t instanceof IllegalStateException) {
197: throw new CollabException(t.getMessage());
198: } else if (t instanceof Exception) {
199: throw (CollabException) t;
200: } else {
201: throw new CollabException(t.toString());
202: }
203: } catch (Exception ex) {
204: throw new CollabException(ex);
205: }
206: }
207: }
208: }
209:
210: private static Method findGetEventID(Class clazz) throws Exception {
211: Method[] methods = clazz.getMethods();
212:
213: for (int i = 0; i < methods.length; i++) {
214: if (methods[i].getName().equals("getEventID")) //NoI18n
215: {
216: return methods[i];
217: }
218: }
219:
220: Debug.log("CollabNotifierConfig", clazz.getName()
221: + "::getEventID() method not found"); //NoI18n
222:
223: return null;
224: }
225: }
|