KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ModuleLifecycleManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss;
21
22 import org.openide.ErrorManager;
23 import org.openide.xml.XMLUtil;
24 import org.openide.filesystems.FileLock;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.Repository;
27 import org.openide.modules.ModuleInstall;
28 import org.openide.util.RequestProcessor;
29 import org.openide.util.NbBundle;
30 import org.w3c.dom.*;
31 import org.xml.sax.*;
32
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.parsers.DocumentBuilder JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import javax.swing.*;
37 import java.io.OutputStream JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41
42 /**
43  * Handles module events distributed by NetBeans module
44  * framework.
45  *
46  * <p>It's registered and instantiated from module manifest.
47  *
48  * @author Petr Kuzel
49  * @author Maros Sandor
50  */

51 public final class ModuleLifecycleManager extends ModuleInstall implements ErrorHandler, EntityResolver {
52
53     static final String JavaDoc [] vcsGenericModules = {
54         "org.netbeans.modules.vcs.advanced", // NOI18N
55
"org.netbeans.modules.vcs.profiles.cvsprofiles", // NOI18N
56
"org.netbeans.modules.vcs.profiles.vss", // NOI18N
57
"org.netbeans.modules.vcs.profiles.pvcs", // NOI18N
58
"org.netbeans.modules.vcs.profiles.subversion", // NOI18N
59
"org.netbeans.modules.vcs.profiles.teamware" // NOI18N
60
};
61     
62     public void restored() {
63         disableOldModules();
64     }
65
66     private void disableOldModules() {
67         Runnable JavaDoc runnable = new Runnable JavaDoc() {
68             public void run() {
69                 boolean notified = false;
70                 outter: for (int i = 0; i < vcsGenericModules.length; i++) {
71                     FileLock lock = null;
72                     OutputStream JavaDoc os = null;
73                     try {
74                         String JavaDoc newModule = vcsGenericModules[i];
75                         String JavaDoc newModuleXML = "Modules/" + newModule.replace('.', '-') + ".xml"; // NOI18N
76
FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(newModuleXML);
77                         if (fo == null) continue;
78                         Document document = readModuleDocument(fo);
79
80                         NodeList list = document.getDocumentElement().getElementsByTagName("param"); // NOI18N
81
int n = list.getLength();
82                         for (int j = 0; j < n; j++) {
83                             Element node = (Element) list.item(j);
84                             if ("enabled".equals(node.getAttribute("name"))) { // NOI18N
85
Text text = (Text) node.getChildNodes().item(0);
86                                 String JavaDoc value = text.getNodeValue();
87                                 if ("true".equals(value)) { // NOI18N
88
text.setNodeValue("false"); // NOI18N
89
break;
90                                 } else {
91                                     continue outter;
92                                 }
93                             }
94                         }
95                         if (!notified) {
96                             JOptionPane.showMessageDialog(null,
97                                                           NbBundle.getBundle(ModuleLifecycleManager.class).getString("MSG_Install_Warning"),
98                                                           NbBundle.getBundle(ModuleLifecycleManager.class).getString("MSG_Install_Warning_Title"),
99                                                           JOptionPane.WARNING_MESSAGE);
100                             notified = true;
101                         }
102                         lock = fo.lock();
103                         os = fo.getOutputStream(lock);
104                         
105                         XMLUtil.write(document, os, "UTF-8"); // NOI18N
106
} catch (Exception JavaDoc e) {
107                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
108                     } finally {
109                         if (os != null) try { os.close(); } catch (IOException JavaDoc ex) {}
110                         if (lock != null) lock.releaseLock();
111                     }
112                 }
113             }
114         };
115         RequestProcessor.getDefault().post(runnable);
116     }
117
118     private Document readModuleDocument(FileObject fo) throws ParserConfigurationException JavaDoc, SAXException, IOException JavaDoc {
119         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
120         dbf.setValidating(false);
121         DocumentBuilder JavaDoc parser = dbf.newDocumentBuilder();
122         parser.setEntityResolver(this);
123         parser.setErrorHandler(this);
124         InputStream JavaDoc is = fo.getInputStream();
125         Document document = parser.parse(is);
126         is.close();
127         return document;
128     }
129
130     public void uninstalled() {
131         CvsVersioningSystem.getInstance().shutdown();
132     }
133
134     public InputSource resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
135         return new InputSource(new ByteArrayInputStream JavaDoc(new byte[0]));
136     }
137     
138     public void error(SAXParseException exception) {
139         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
140     }
141
142     public void fatalError(SAXParseException exception) {
143         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
144     }
145
146     public void warning(SAXParseException exception) {
147         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
148     }
149 }
150
Popular Tags