KickJava   Java API By Example, From Geeks To Geeks.

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