versioning.system.cvss/src/org/netbeans/modules/versioning/system/cvss/ModuleLifecycleManager.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun Nov 22 11:35:02 2009 +0100 (10 hours ago)
changeset 154033 fa8b12981306
parent 11372878e10891a932
permissions -rw-r--r--
Merge RP javadoc improvements
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * Contributor(s):
    25  *
    26  * The Original Software is NetBeans. The Initial Developer of the Original
    27  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    28  * Microsystems, Inc. All Rights Reserved.
    29  *
    30  * If you wish your version of this file to be governed by only the CDDL
    31  * or only the GPL Version 2, indicate your decision by adding
    32  * "[Contributor] elects to include this software in this distribution
    33  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    34  * single choice of license, a recipient has the option to distribute
    35  * your version of this file under either the CDDL, the GPL Version 2 or
    36  * to extend the choice of license to its licensees as provided above.
    37  * However, if you add GPL Version 2 code and therefore, elected the GPL
    38  * Version 2 license, then the option applies only if the new code is
    39  * made subject to such option by the copyright holder.
    40  */
    41 
    42 package org.netbeans.modules.versioning.system.cvss;
    43 
    44 import org.openide.ErrorManager;
    45 import org.openide.xml.XMLUtil;
    46 import org.openide.filesystems.FileLock;
    47 import org.openide.filesystems.FileObject;
    48 import org.openide.modules.ModuleInstall;
    49 import org.openide.util.RequestProcessor;
    50 import org.openide.util.NbBundle;
    51 import org.w3c.dom.*;
    52 import org.xml.sax.*;
    53 
    54 import javax.xml.parsers.DocumentBuilderFactory;
    55 import javax.xml.parsers.DocumentBuilder;
    56 import javax.xml.parsers.ParserConfigurationException;
    57 import javax.swing.*;
    58 import java.io.OutputStream;
    59 import java.io.InputStream;
    60 import java.io.IOException;
    61 import java.io.ByteArrayInputStream;
    62 import org.openide.filesystems.FileUtil;
    63 
    64 /**
    65  * Handles module events distributed by NetBeans module
    66  * framework.
    67  *
    68  * <p>It's registered and instantiated from module manifest.
    69  *
    70  * @author Petr Kuzel
    71  * @author Maros Sandor
    72  */
    73 public final class ModuleLifecycleManager extends ModuleInstall implements ErrorHandler, EntityResolver {
    74 
    75     static final String [] vcsGenericModules = {
    76         "org.netbeans.modules.vcs.advanced", // NOI18N
    77         "org.netbeans.modules.vcs.profiles.cvsprofiles", // NOI18N
    78         "org.netbeans.modules.vcs.profiles.vss", // NOI18N
    79         "org.netbeans.modules.vcs.profiles.pvcs", // NOI18N
    80         "org.netbeans.modules.vcs.profiles.subversion", // NOI18N
    81         "org.netbeans.modules.vcs.profiles.teamware" // NOI18N
    82     };
    83     
    84     public void restored() {
    85         disableOldModules();
    86     }
    87 
    88     private void disableOldModules() {
    89         Runnable runnable = new Runnable() {
    90             public void run() {
    91                 boolean notified = false;
    92                 outter: for (int i = 0; i < vcsGenericModules.length; i++) {
    93                     FileLock lock = null;
    94                     OutputStream os = null;
    95                     try {
    96                         String newModule = vcsGenericModules[i];
    97                         String newModuleXML = "Modules/" + newModule.replace('.', '-') + ".xml"; // NOI18N
    98                         FileObject fo = FileUtil.getConfigFile(newModuleXML);
    99                         if (fo == null) continue;
   100                         Document document = readModuleDocument(fo);
   101 
   102                         NodeList list = document.getDocumentElement().getElementsByTagName("param"); // NOI18N
   103                         int n = list.getLength();
   104                         for (int j = 0; j < n; j++) {
   105                             Element node = (Element) list.item(j);
   106                             if ("enabled".equals(node.getAttribute("name"))) { // NOI18N
   107                                 Text text = (Text) node.getChildNodes().item(0);
   108                                 String value = text.getNodeValue();
   109                                 if ("true".equals(value)) { // NOI18N
   110                                     text.setNodeValue("false"); // NOI18N
   111                                     break;
   112                                 } else {
   113                                     continue outter;
   114                                 }
   115                             }
   116                         }
   117                         if (!notified) {
   118                             JOptionPane.showMessageDialog(null, 
   119                                                           NbBundle.getBundle(ModuleLifecycleManager.class).getString("MSG_Install_Warning"), 
   120                                                           NbBundle.getBundle(ModuleLifecycleManager.class).getString("MSG_Install_Warning_Title"), 
   121                                                           JOptionPane.WARNING_MESSAGE);
   122                             notified = true;
   123                         }
   124                         lock = fo.lock();
   125                         os = fo.getOutputStream(lock);
   126                         
   127                         XMLUtil.write(document, os, "UTF-8"); // NOI18N
   128                     } catch (Exception e) {
   129                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
   130                     } finally {
   131                         if (os != null) try { os.close(); } catch (IOException ex) {}
   132                         if (lock != null) lock.releaseLock();
   133                     }
   134                 }
   135             }
   136         };
   137         RequestProcessor.getDefault().post(runnable);
   138     }
   139 
   140     private Document readModuleDocument(FileObject fo) throws ParserConfigurationException, SAXException, IOException {
   141         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   142         dbf.setValidating(false);
   143         DocumentBuilder parser = dbf.newDocumentBuilder();
   144         parser.setEntityResolver(this);
   145         parser.setErrorHandler(this);
   146         InputStream is = fo.getInputStream();
   147         Document document = parser.parse(is);
   148         is.close();
   149         return document;
   150     }
   151 
   152     public void uninstalled() {
   153         CvsVersioningSystem.getInstance().shutdown();
   154     }
   155 
   156     public InputSource resolveEntity(String publicId, String systemId) {
   157         return new InputSource(new ByteArrayInputStream(new byte[0]));
   158     }
   159     
   160     public void error(SAXParseException exception) {
   161         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
   162     }
   163 
   164     public void fatalError(SAXParseException exception) {
   165         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
   166     }
   167 
   168     public void warning(SAXParseException exception) {
   169         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
   170     }
   171 }