Source Code Cross Referenced for Util.java in  » IDE-Netbeans » web.core » org » netbeans » modules » websvc » wsitconf » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » web.core » org.netbeans.modules.websvc.wsitconf.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         *
0004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005:         *
0006:         * The contents of this file are subject to the terms of either the GNU
0007:         * General Public License Version 2 only ("GPL") or the Common
0008:         * Development and Distribution License("CDDL") (collectively, the
0009:         * "License"). You may not use this file except in compliance with the
0010:         * License. You can obtain a copy of the License at
0011:         * http://www.netbeans.org/cddl-gplv2.html
0012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013:         * specific language governing permissions and limitations under the
0014:         * License.  When distributing the software, include this License Header
0015:         * Notice in each file and include the License file at
0016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
0017:         * particular file as subject to the "Classpath" exception as provided
0018:         * by Sun in the GPL Version 2 section of the License file that
0019:         * accompanied this code. If applicable, add the following below the
0020:         * License Header, with the fields enclosed by brackets [] replaced by
0021:         * your own identifying information:
0022:         * "Portions Copyrighted [year] [name of copyright owner]"
0023:         *
0024:         * Contributor(s):
0025:         *
0026:         * The Original Software is NetBeans. The Initial Developer of the Original
0027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028:         * Microsystems, Inc. All Rights Reserved.
0029:         *
0030:         * If you wish your version of this file to be governed by only the CDDL
0031:         * or only the GPL Version 2, indicate your decision by adding
0032:         * "[Contributor] elects to include this software in this distribution
0033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034:         * single choice of license, a recipient has the option to distribute
0035:         * your version of this file under either the CDDL, the GPL Version 2 or
0036:         * to extend the choice of license to its licensees as provided above.
0037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
0038:         * Version 2 license, then the option applies only if the new code is
0039:         * made subject to such option by the copyright holder.
0040:         */
0041:
0042:        package org.netbeans.modules.websvc.wsitconf.util;
0043:
0044:        import java.util.logging.Logger;
0045:        import java.awt.Component;
0046:        import java.util.logging.Level;
0047:        import javax.swing.JLabel;
0048:        import java.awt.Container;
0049:        import java.io.BufferedReader;
0050:        import java.io.BufferedWriter;
0051:        import java.io.File;
0052:        import java.io.FileNotFoundException;
0053:        import java.io.IOException;
0054:        import java.net.URL;
0055:        import java.security.KeyStoreException;
0056:        import java.security.NoSuchAlgorithmException;
0057:        import java.security.cert.CertificateException;
0058:        import javax.swing.JComponent;
0059:        import java.util.*;
0060:        import org.netbeans.api.java.classpath.ClassPath;
0061:        import org.netbeans.api.java.project.JavaProjectConstants;
0062:        import org.netbeans.api.java.queries.UnitTestForSourceQuery;
0063:        import org.netbeans.api.project.Project;
0064:        import org.netbeans.api.project.ProjectUtils;
0065:        import org.netbeans.api.project.SourceGroup;
0066:        import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
0067:        import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
0068:        import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
0069:        import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
0070:        import org.openide.filesystems.FileObject;
0071:        import org.openide.filesystems.URLMapper;
0072:        import java.io.FileInputStream;
0073:        import java.io.FileOutputStream;
0074:        import java.io.FileReader;
0075:        import java.io.FileWriter;
0076:        import java.io.InputStream;
0077:        import java.io.OutputStream;
0078:        import java.security.Key;
0079:        import java.security.KeyStore;
0080:        import java.security.cert.Certificate;
0081:        import javax.xml.namespace.QName;
0082:        import javax.xml.parsers.DocumentBuilder;
0083:        import javax.xml.parsers.DocumentBuilderFactory;
0084:        import javax.xml.transform.OutputKeys;
0085:        import javax.xml.transform.Transformer;
0086:        import javax.xml.transform.TransformerFactory;
0087:        import javax.xml.transform.dom.DOMSource;
0088:        import javax.xml.transform.stream.StreamResult;
0089:        import org.netbeans.modules.j2ee.dd.api.web.Servlet;
0090:        import org.netbeans.modules.j2ee.dd.api.web.WebApp;
0091:        import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
0092:        import org.netbeans.modules.websvc.jaxwsruntimemodel.JavaWsdlMapper;
0093:        import org.netbeans.modules.websvc.wsitconf.ui.service.profiles.UsernameAuthenticationProfile;
0094:        import org.netbeans.modules.websvc.wsitconf.ui.service.subpanels.KeystorePanel;
0095:        import org.netbeans.modules.xml.retriever.catalog.Utilities;
0096:        import org.netbeans.modules.xml.wsdl.model.Binding;
0097:        import org.netbeans.modules.xml.wsdl.model.BindingOperation;
0098:        import org.netbeans.modules.xml.wsdl.model.Definitions;
0099:        import org.netbeans.modules.xml.wsdl.model.PortType;
0100:        import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory;
0101:        import org.netbeans.modules.xml.wsdl.model.WSDLModel;
0102:        import org.netbeans.modules.xml.xam.ModelSource;
0103:        import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
0104:        import org.openide.filesystems.FileSystem;
0105:        import org.openide.filesystems.FileUtil;
0106:        import org.openide.filesystems.Repository;
0107:        import org.openide.loaders.DataFolder;
0108:        import org.openide.loaders.DataObject;
0109:        import org.openide.util.Exceptions;
0110:        import org.w3c.dom.Document;
0111:        import org.w3c.dom.Element;
0112:        import org.w3c.dom.Node;
0113:        import org.w3c.dom.NodeList;
0114:
0115:        public class Util {
0116:            private static final String LOGGER_GLOBAL = "global";
0117:            private static final String WSSIP = "wssip";
0118:            private static final String XWS_SECURITY_CLIENT = "xws-security-client";
0119:            private static final String XWS_SECURITY_SERVER = "xws-security-server";
0120:
0121:            private static final String SERVER_KEYSTORE_BUNDLED = "/org/netbeans/modules/websvc/wsitconf/resources/server-keystore.jks"; //NOI18N
0122:            private static final String SERVER_TRUSTSTORE_BUNDLED = "/org/netbeans/modules/websvc/wsitconf/resources/server-truststore.jks"; //NOI18N
0123:            private static final String CLIENT_KEYSTORE_BUNDLED = "/org/netbeans/modules/websvc/wsitconf/resources/client-keystore.jks"; //NOI18N
0124:            private static final String CLIENT_TRUSTSTORE_BUNDLED = "/org/netbeans/modules/websvc/wsitconf/resources/client-truststore.jks"; //NOI18N
0125:
0126:            private static final String PASSWORD = "changeit";
0127:
0128:            private static final String STORE_FOLDER_NAME = "certs";
0129:
0130:            public enum ProjectType {
0131:                WEB, EJB, CLIENT, UNKNOWN
0132:            };
0133:
0134:            private static final Logger logger = Logger.getLogger(Util.class
0135:                    .getName());
0136:
0137:            /*
0138:             * Changes the text of a JLabel in component from oldLabel to newLabel
0139:             */
0140:            public static void changeLabelInComponent(JComponent component,
0141:                    String oldLabel, String newLabel) {
0142:                JLabel label = findLabel(component, oldLabel);
0143:                if (label != null) {
0144:                    label.setText(newLabel);
0145:                }
0146:            }
0147:
0148:            /*
0149:             * Hides a JLabel and the component that it is designated to labelFor, if any
0150:             */
0151:            public static void hideLabelAndLabelFor(JComponent component,
0152:                    String lab) {
0153:                JLabel label = findLabel(component, lab);
0154:                if (label != null) {
0155:                    label.setVisible(false);
0156:                    Component c = label.getLabelFor();
0157:                    if (c != null) {
0158:                        c.setVisible(false);
0159:                    }
0160:                }
0161:            }
0162:
0163:            /*
0164:             * Recursively gets all components in the components array and puts it in allComponents
0165:             */
0166:            public static void getAllComponents(Component[] components,
0167:                    Collection<Component> allComponents) {
0168:                for (int i = 0; i < components.length; i++) {
0169:                    if (components[i] != null) {
0170:                        allComponents.add(components[i]);
0171:                        if (((Container) components[i]).getComponentCount() != 0) {
0172:                            getAllComponents(((Container) components[i])
0173:                                    .getComponents(), allComponents);
0174:                        }
0175:                    }
0176:                }
0177:            }
0178:
0179:            /*
0180:             *  Recursively finds a JLabel that has labelText in comp
0181:             */
0182:            public static JLabel findLabel(JComponent comp, String labelText) {
0183:                ArrayList<Component> allComponents = new ArrayList<Component>();
0184:                getAllComponents(comp.getComponents(), allComponents);
0185:                Iterator iterator = allComponents.iterator();
0186:                while (iterator.hasNext()) {
0187:                    Component c = (Component) iterator.next();
0188:                    if (c instanceof  JLabel) {
0189:                        JLabel label = (JLabel) c;
0190:                        if (label.getText().equals(labelText)) {
0191:                            return label;
0192:                        }
0193:                    }
0194:                }
0195:                return null;
0196:            }
0197:
0198:            /**
0199:             * Returns Java source groups for all source packages in given project.<br>
0200:             * Doesn't include test packages.
0201:             *
0202:             * @param project Project to search
0203:             * @return Array of SourceGroup. It is empty if any probelm occurs.
0204:             */
0205:            public static SourceGroup[] getJavaSourceGroups(Project project) {
0206:                SourceGroup[] sourceGroups = ProjectUtils
0207:                        .getSources(project)
0208:                        .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
0209:                Set testGroups = getTestSourceGroups(sourceGroups);
0210:                List<SourceGroup> result = new ArrayList<SourceGroup>();
0211:                for (int i = 0; i < sourceGroups.length; i++) {
0212:                    if (!testGroups.contains(sourceGroups[i])) {
0213:                        result.add(sourceGroups[i]);
0214:                    }
0215:                }
0216:                return result.toArray(new SourceGroup[result.size()]);
0217:            }
0218:
0219:            private static Set<SourceGroup> getTestSourceGroups(
0220:                    SourceGroup[] sourceGroups) {
0221:                Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
0222:                Set<SourceGroup> testGroups = new HashSet<SourceGroup>();
0223:                for (int i = 0; i < sourceGroups.length; i++) {
0224:                    testGroups.addAll(getTestTargets(sourceGroups[i],
0225:                            foldersToSourceGroupsMap));
0226:                }
0227:                return testGroups;
0228:            }
0229:
0230:            private static Map createFoldersToSourceGroupsMap(
0231:                    final SourceGroup[] sourceGroups) {
0232:                Map<FileObject, SourceGroup> result;
0233:                if (sourceGroups.length == 0) {
0234:                    result = Collections.emptyMap();
0235:                } else {
0236:                    result = new HashMap<FileObject, SourceGroup>(
0237:                            2 * sourceGroups.length, .5f);
0238:                    for (int i = 0; i < sourceGroups.length; i++) {
0239:                        SourceGroup sourceGroup = sourceGroups[i];
0240:                        result.put(sourceGroup.getRootFolder(), sourceGroup);
0241:                    }
0242:                }
0243:                return result;
0244:            }
0245:
0246:            private static List<FileObject> getFileObjects(URL[] urls) {
0247:                List<FileObject> result = new ArrayList<FileObject>();
0248:                for (int i = 0; i < urls.length; i++) {
0249:                    FileObject sourceRoot = URLMapper.findFileObject(urls[i]);
0250:                    if (sourceRoot != null) {
0251:                        result.add(sourceRoot);
0252:                    } else {
0253:                        if (logger.isLoggable(Level.INFO)) {
0254:                            logger.log(Level.INFO,
0255:                                    "No FileObject found for the following URL: "
0256:                                            + urls[i]); //NOI18N
0257:                        }
0258:                    }
0259:                }
0260:                return result;
0261:            }
0262:
0263:            private static List<SourceGroup> getTestTargets(
0264:                    SourceGroup sourceGroup, Map foldersToSourceGroupsMap) {
0265:                final URL[] rootURLs = UnitTestForSourceQuery
0266:                        .findUnitTests(sourceGroup.getRootFolder());
0267:                if (rootURLs.length == 0) {
0268:                    return Collections.emptyList();
0269:                }
0270:                List<SourceGroup> result = new ArrayList<SourceGroup>();
0271:                List<FileObject> sourceRoots = getFileObjects(rootURLs);
0272:                for (int i = 0; i < sourceRoots.size(); i++) {
0273:                    FileObject sourceRoot = sourceRoots.get(i);
0274:                    SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap
0275:                            .get(sourceRoot);
0276:                    if (srcGroup != null) {
0277:                        result.add(srcGroup);
0278:                    }
0279:                }
0280:                return result;
0281:            }
0282:
0283:            /* Used to validate number inputs
0284:             */
0285:            public static boolean isPositiveNumber(String s,
0286:                    boolean zeroAllowed, boolean allowEmptyValue) {
0287:                Integer i = null;
0288:                if ((s == null) || ("".equals(s))) {
0289:                    return allowEmptyValue ? true : false;
0290:                }
0291:                try {
0292:                    i = Integer.parseInt(s);
0293:                    if (i != null) {
0294:                        if (zeroAllowed) {
0295:                            return i.intValue() >= 0;
0296:                        }
0297:                        return i.intValue() > 0;
0298:                    }
0299:                } catch (NumberFormatException nfe) {
0300:                    logger.log(Level.FINE, null, nfe); // just ignore
0301:                }
0302:                return false;
0303:            }
0304:
0305:            public static List<String> getAliases(String storePath,
0306:                    char[] password, String type) throws IOException {
0307:                if ((storePath == null) || (type == null))
0308:                    return null;
0309:                FileInputStream iStream = null;
0310:                try {
0311:                    File f = new File(storePath);
0312:                    if ((f == null) || (!f.exists())) {
0313:                        throw new IOException();
0314:                    }
0315:                    iStream = new FileInputStream(f);
0316:                    java.security.KeyStore keyStore;
0317:                    keyStore = java.security.KeyStore.getInstance(type);
0318:                    keyStore.load(iStream, password);
0319:                    Enumeration<String> e = keyStore.aliases();
0320:                    ArrayList<String> arr = new ArrayList<String>(keyStore
0321:                            .size());
0322:                    while (e.hasMoreElements()) {
0323:                        String key = e.nextElement();
0324:                        arr.add(key);
0325:                    }
0326:                    Collections.sort(arr);
0327:                    return arr;
0328:                } catch (FileNotFoundException ex) {
0329:                    logger.log(Level.INFO, null, ex);
0330:                } catch (KeyStoreException ex) {
0331:                    logger.log(Level.INFO, null, ex);
0332:                } catch (NoSuchAlgorithmException ex) {
0333:                    logger.log(Level.INFO, null, ex);
0334:                } catch (CertificateException ex) {
0335:                    logger.log(Level.INFO, null, ex);
0336:                } finally {
0337:                    if (iStream != null)
0338:                        iStream.close();
0339:                }
0340:                return null;
0341:            }
0342:
0343:            public static final String getPassword(Project p) {
0344:                J2eeModuleProvider mp = p.getLookup().lookup(
0345:                        J2eeModuleProvider.class);
0346:                if (mp != null) {
0347:                    InstanceProperties ip = mp.getInstanceProperties();
0348:                    return ip.getProperty(InstanceProperties.PASSWORD_ATTR);
0349:                }
0350:                return "";
0351:            }
0352:
0353:            private static String getServerInstanceID(Project p) {
0354:                if (p != null) {
0355:                    J2eeModuleProvider mp = p.getLookup().lookup(
0356:                            J2eeModuleProvider.class);
0357:                    if (mp != null) {
0358:                        return mp.getServerInstanceID();
0359:                    }
0360:                }
0361:                return null;
0362:            }
0363:
0364:            public static final boolean isWsitSupported(Project p) {
0365:
0366:                // check if the wsimport class is already present - this means we don't need to add the library
0367:                SourceGroup[] sgs = ProjectUtils.getSources(p).getSourceGroups(
0368:                        JavaProjectConstants.SOURCES_TYPE_JAVA);
0369:                ClassPath classPath = ClassPath.getClassPath(sgs[0]
0370:                        .getRootFolder(), ClassPath.COMPILE);
0371:                FileObject wsimportFO = classPath
0372:                        .findResource("com/sun/xml/ws/policy/Policy.class"); // NOI18N
0373:
0374:                if (wsimportFO == null) {
0375:                    J2eePlatform j2eePlatform = getJ2eePlatform(p);
0376:                    if (j2eePlatform != null) {
0377:                        return j2eePlatform
0378:                                .isToolSupported(J2eePlatform.TOOL_WSIT); //NOI18N
0379:                    }
0380:                }
0381:                return true;
0382:            }
0383:
0384:            public static J2eePlatform getJ2eePlatform(Project project) {
0385:                String serverInstanceID = getServerInstanceID(project);
0386:                if ((serverInstanceID != null)
0387:                        && (serverInstanceID.length() > 0)) {
0388:                    return Deployment.getDefault().getJ2eePlatform(
0389:                            serverInstanceID);
0390:                }
0391:                return null;
0392:            }
0393:
0394:            /**
0395:             * Is J2EE version of a given project JavaEE 5 or higher?
0396:             *
0397:             * @param project J2EE project
0398:             * @return true if J2EE version is JavaEE 5 or higher; otherwise false
0399:             */
0400:            public static boolean isJavaEE5orHigher(Project project) {
0401:                if (project == null) {
0402:                    return false;
0403:                }
0404:                J2eeModuleProvider j2eeModuleProvider = project.getLookup()
0405:                        .lookup(J2eeModuleProvider.class);
0406:                if (j2eeModuleProvider != null) {
0407:                    J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule();
0408:                    if (j2eeModule != null) {
0409:                        Object type = j2eeModule.getModuleType();
0410:                        double version = Double.parseDouble(j2eeModule
0411:                                .getModuleVersion());
0412:                        if (J2eeModule.EJB.equals(type) && (version > 2.1)) {
0413:                            return true;
0414:                        }
0415:                        if (J2eeModule.WAR.equals(type) && (version > 2.4)) {
0416:                            return true;
0417:                        }
0418:                        if (J2eeModule.CLIENT.equals(type) && (version > 1.4)) {
0419:                            return true;
0420:                        }
0421:                    }
0422:                }
0423:                return false;
0424:            }
0425:
0426:            public static String getServerName(Project p) {
0427:                String sID = getServerInstanceID(p);
0428:                if (sID != null) {
0429:                    J2eePlatform j2eePlatform = Deployment.getDefault()
0430:                            .getJ2eePlatform(sID);
0431:                    return j2eePlatform.getDisplayName();
0432:                }
0433:                return null;
0434:            }
0435:
0436:            public static String getStoreLocation(Project project,
0437:                    boolean trust, boolean client) {
0438:                String storeLocation = null;
0439:                J2eeModuleProvider mp = project.getLookup().lookup(
0440:                        J2eeModuleProvider.class);
0441:                if (mp != null) {
0442:                    String sID = mp.getServerInstanceID();
0443:                    InstanceProperties ip = mp.getInstanceProperties();
0444:                    if ((ip == null) || ("".equals(ip.getProperty("LOCATION")))) {
0445:                        return "";
0446:                    }
0447:                    storeLocation = getStoreLocation(sID, trust, client);
0448:                }
0449:                return storeLocation;
0450:            }
0451:
0452:            private static String getStoreLocation(String serverInstanceID,
0453:                    boolean trust, boolean client) {
0454:                String storeLocation = null;
0455:                J2eePlatform j2eePlatform = Deployment.getDefault()
0456:                        .getJ2eePlatform(serverInstanceID);
0457:                File[] keyLocs = null;
0458:                String store = null;
0459:                if (client) {
0460:                    store = trust ? J2eePlatform.TOOL_TRUSTSTORE_CLIENT
0461:                            : J2eePlatform.TOOL_KEYSTORE_CLIENT;
0462:                } else {
0463:                    store = trust ? J2eePlatform.TOOL_TRUSTSTORE
0464:                            : J2eePlatform.TOOL_KEYSTORE;
0465:                }
0466:                keyLocs = j2eePlatform.getToolClasspathEntries(store);
0467:                if ((keyLocs != null) && (keyLocs.length > 0)) {
0468:                    storeLocation = keyLocs[0].getAbsolutePath();
0469:                }
0470:                return storeLocation;
0471:            }
0472:
0473:            public static FileObject getTomcatLocation(Project project) {
0474:                J2eeModuleProvider mp = project.getLookup().lookup(
0475:                        J2eeModuleProvider.class);
0476:                FileObject folder = null;
0477:                String id = null;
0478:                if (mp != null) {
0479:                    try {
0480:                        id = mp.getServerInstanceID();
0481:                        folder = getTomcatLocation(id);
0482:                    } catch (Exception ex) {
0483:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, id, ex);
0484:                    }
0485:                }
0486:                return folder;
0487:            }
0488:
0489:            public static FileObject getTomcatLocation(String serverID) {
0490:                if (serverID == null)
0491:                    return null;
0492:                FileObject folder = null;
0493:                try {
0494:                    int begin = serverID.indexOf("home=") + 5;
0495:                    int end = serverID.indexOf(":",
0496:                            serverID.indexOf("home=") + 1);
0497:                    if (end <= begin) {
0498:                        end = serverID.length();
0499:                    }
0500:                    String location = serverID.substring(begin, end);
0501:                    File f = new File(location);
0502:                    if (f != null) {
0503:                        folder = FileUtil.toFileObject(f);
0504:                    }
0505:                } catch (Exception ex) {
0506:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, serverID,
0507:                            ex);
0508:                }
0509:                return folder;
0510:            }
0511:
0512:            public static final void fillDefaultsToDefaultServer() {
0513:
0514:                String sID = Deployment.getDefault()
0515:                        .getDefaultServerInstanceID();
0516:                boolean glassfish = isGlassfish(sID);
0517:
0518:                String serverKeyStorePath = getStoreLocation(sID, false, false);
0519:                String serverTrustStorePath = getStoreLocation(sID, true, false);
0520:                String clientKeyStorePath = getStoreLocation(sID, false, true);
0521:                String clientTrustStorePath = getStoreLocation(sID, true, true);
0522:
0523:                if (!glassfish) {
0524:                    FileObject tomcatLocation = getTomcatLocation(sID);
0525:                    try {
0526:                        FileObject targetFolder = FileUtil.createFolder(
0527:                                tomcatLocation, STORE_FOLDER_NAME);
0528:                        DataFolder folderDO = (DataFolder) DataObject
0529:                                .find(targetFolder);
0530:                        FileSystem fs = Repository.getDefault()
0531:                                .getDefaultFileSystem();
0532:                        FileObject foClientKey = fs
0533:                                .findResource("Templates/WebServices/client-keystore.jks"); // NOI18N
0534:                        FileObject foClientTrust = fs
0535:                                .findResource("Templates/WebServices/client-truststore.jks"); // NOI18N
0536:                        FileObject foServerKey = fs
0537:                                .findResource("Templates/WebServices/server-keystore.jks"); // NOI18N
0538:                        FileObject foServerTrust = fs
0539:                                .findResource("Templates/WebServices/server-truststore.jks"); // NOI18N
0540:                        FileObject[] filesToCreate = { foClientKey,
0541:                                foClientTrust, foServerKey, foServerTrust };
0542:                        for (FileObject fo : filesToCreate) {
0543:                            if (fo != null) {
0544:                                DataObject template = DataObject.find(fo);
0545:                                if (template != null) {
0546:                                    if (targetFolder.getFileObject(
0547:                                            fo.getName(), fo.getExt()) == null) {
0548:                                        template.createFromTemplate(folderDO,
0549:                                                fo.getNameExt());
0550:                                    }
0551:                                }
0552:                            }
0553:                        }
0554:
0555:                        FileObject tomcatUsers = tomcatLocation
0556:                                .getFileObject("conf/tomcat-users.xml");
0557:
0558:                        DocumentBuilderFactory dbf = DocumentBuilderFactory
0559:                                .newInstance();
0560:                        DocumentBuilder builder = dbf.newDocumentBuilder();
0561:                        Document document = builder.parse(FileUtil
0562:                                .toFile(tomcatUsers));
0563:
0564:                        NodeList nodes = document
0565:                                .getElementsByTagName("tomcat-users");
0566:                        if ((nodes != null) && (nodes.getLength() > 0)) {
0567:                            Node n = nodes.item(0);
0568:                            NodeList users = document
0569:                                    .getElementsByTagName("user");
0570:                            boolean foundUser = false;
0571:                            for (int i = 0; i < users.getLength(); i++) {
0572:                                Node node = users.item(i);
0573:                                if (node instanceof  Element) {
0574:                                    Element u = (Element) node;
0575:                                    String userAttr = u.getAttribute("name");
0576:                                    if (UsernameAuthenticationProfile.DEFAULT_USERNAME
0577:                                            .equals(userAttr)) {
0578:                                        foundUser = true;
0579:                                        break;
0580:                                    }
0581:                                }
0582:                            }
0583:                            if (!foundUser) {
0584:                                if (tomcatUsers.getParent().getFileObject(
0585:                                        "tomcat-users.backup", "xml") == null) {
0586:                                    FileUtil
0587:                                            .copyFile(tomcatUsers, tomcatUsers
0588:                                                    .getParent(),
0589:                                                    "tomcat-users.backup");
0590:                                }
0591:
0592:                                Element user = document.createElement("user");
0593:                                user
0594:                                        .setAttribute(
0595:                                                "name",
0596:                                                UsernameAuthenticationProfile.DEFAULT_USERNAME);
0597:                                user
0598:                                        .setAttribute(
0599:                                                "password",
0600:                                                UsernameAuthenticationProfile.DEFAULT_PASSWORD);
0601:                                user.setAttribute("roles", "tomcat");
0602:                                n.appendChild(user);
0603:
0604:                                Transformer transformer = TransformerFactory
0605:                                        .newInstance().newTransformer();
0606:                                transformer.setOutputProperty(
0607:                                        OutputKeys.INDENT, "yes");
0608:
0609:                                //initialize StreamResult with File object to save to file
0610:                                StreamResult result = new StreamResult(FileUtil
0611:                                        .toFile(tomcatUsers));
0612:                                DOMSource source = new DOMSource(document);
0613:                                transformer.transform(source, result);
0614:                            }
0615:                        }
0616:                    } catch (Exception ex) {
0617:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null,
0618:                                ex);
0619:                    }
0620:                    return;
0621:                }
0622:
0623:                String dstPasswd = getDefaultPassword(sID);
0624:                try {
0625:                    copyKey(SERVER_KEYSTORE_BUNDLED, XWS_SECURITY_SERVER,
0626:                            PASSWORD, PASSWORD, serverKeyStorePath,
0627:                            XWS_SECURITY_SERVER, dstPasswd, false);
0628:                    copyKey(SERVER_KEYSTORE_BUNDLED, WSSIP, PASSWORD, PASSWORD,
0629:                            serverKeyStorePath, WSSIP, dstPasswd, false);
0630:                    copyKey(SERVER_TRUSTSTORE_BUNDLED, "certificate-authority",
0631:                            PASSWORD, PASSWORD, serverTrustStorePath,
0632:                            "xwss-certificate-authority", dstPasswd, true);
0633:                    copyKey(SERVER_TRUSTSTORE_BUNDLED, XWS_SECURITY_CLIENT,
0634:                            PASSWORD, PASSWORD, serverTrustStorePath,
0635:                            XWS_SECURITY_CLIENT, dstPasswd, true);
0636:                    copyKey(CLIENT_KEYSTORE_BUNDLED, XWS_SECURITY_CLIENT,
0637:                            PASSWORD, PASSWORD, clientKeyStorePath,
0638:                            XWS_SECURITY_CLIENT, dstPasswd, false);
0639:                    copyKey(CLIENT_TRUSTSTORE_BUNDLED, XWS_SECURITY_SERVER,
0640:                            PASSWORD, PASSWORD, clientTrustStorePath,
0641:                            XWS_SECURITY_SERVER, dstPasswd, true);
0642:                    copyKey(CLIENT_TRUSTSTORE_BUNDLED, WSSIP, PASSWORD,
0643:                            PASSWORD, clientTrustStorePath, WSSIP, dstPasswd,
0644:                            true);
0645:                } catch (Exception ex) {
0646:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null, ex);
0647:                }
0648:            }
0649:
0650:            public static final void fillDefaults(Project project,
0651:                    boolean client, boolean refreshScript) {
0652:
0653:                boolean tomcat = isTomcat(project);
0654:                boolean glassfish = isGlassfish(project);
0655:
0656:                if (tomcat) {
0657:                    if (project != null) {
0658:                        FileObject tomcatLocation = getTomcatLocation(project);
0659:                        try {
0660:                            FileObject targetFolder = FileUtil.createFolder(
0661:                                    tomcatLocation, STORE_FOLDER_NAME);
0662:                            DataFolder folderDO = (DataFolder) DataObject
0663:                                    .find(targetFolder);
0664:                            FileSystem fs = Repository.getDefault()
0665:                                    .getDefaultFileSystem();
0666:                            FileObject foClientKey = fs
0667:                                    .findResource("Templates/WebServices/client-keystore.jks"); // NOI18N
0668:                            FileObject foClientTrust = fs
0669:                                    .findResource("Templates/WebServices/client-truststore.jks"); // NOI18N
0670:                            FileObject foServerKey = fs
0671:                                    .findResource("Templates/WebServices/server-keystore.jks"); // NOI18N
0672:                            FileObject foServerTrust = fs
0673:                                    .findResource("Templates/WebServices/server-truststore.jks"); // NOI18N
0674:                            FileObject[] filesToCreate = { foClientKey,
0675:                                    foClientTrust, foServerKey, foServerTrust };
0676:                            for (FileObject fo : filesToCreate) {
0677:                                if (fo != null) {
0678:                                    DataObject template = DataObject.find(fo);
0679:                                    if (template != null) {
0680:                                        if (targetFolder.getFileObject(fo
0681:                                                .getName(), fo.getExt()) == null) {
0682:                                            template.createFromTemplate(
0683:                                                    folderDO, fo.getNameExt());
0684:                                        }
0685:                                    }
0686:                                }
0687:                            }
0688:
0689:                            if (!client) {
0690:                                FileObject tomcatUsers = tomcatLocation
0691:                                        .getFileObject("conf/tomcat-users.xml");
0692:
0693:                                DocumentBuilderFactory dbf = DocumentBuilderFactory
0694:                                        .newInstance();
0695:                                DocumentBuilder builder = dbf
0696:                                        .newDocumentBuilder();
0697:                                Document document = builder.parse(FileUtil
0698:                                        .toFile(tomcatUsers));
0699:
0700:                                NodeList nodes = document
0701:                                        .getElementsByTagName("tomcat-users");
0702:                                if ((nodes != null) && (nodes.getLength() > 0)) {
0703:                                    Node n = nodes.item(0);
0704:                                    NodeList users = document
0705:                                            .getElementsByTagName("user");
0706:                                    boolean foundUser = false;
0707:                                    for (int i = 0; i < users.getLength(); i++) {
0708:                                        Node node = users.item(i);
0709:                                        if (node instanceof  Element) {
0710:                                            Element u = (Element) node;
0711:                                            String userAttr = u
0712:                                                    .getAttribute("name");
0713:                                            if (UsernameAuthenticationProfile.DEFAULT_USERNAME
0714:                                                    .equals(userAttr)) {
0715:                                                foundUser = true;
0716:                                                break;
0717:                                            }
0718:                                        }
0719:                                    }
0720:                                    if (!foundUser) {
0721:                                        if (tomcatUsers.getParent()
0722:                                                .getFileObject(
0723:                                                        "tomcat-users.backup",
0724:                                                        "xml") == null) {
0725:                                            FileUtil.copyFile(tomcatUsers,
0726:                                                    tomcatUsers.getParent(),
0727:                                                    "tomcat-users.backup");
0728:                                        }
0729:
0730:                                        Element user = document
0731:                                                .createElement("user");
0732:                                        user
0733:                                                .setAttribute(
0734:                                                        "name",
0735:                                                        UsernameAuthenticationProfile.DEFAULT_USERNAME);
0736:                                        user
0737:                                                .setAttribute(
0738:                                                        "password",
0739:                                                        UsernameAuthenticationProfile.DEFAULT_PASSWORD);
0740:                                        user.setAttribute("roles", "tomcat");
0741:                                        n.appendChild(user);
0742:
0743:                                        Transformer transformer = TransformerFactory
0744:                                                .newInstance().newTransformer();
0745:                                        transformer.setOutputProperty(
0746:                                                OutputKeys.INDENT, "yes");
0747:
0748:                                        //initialize StreamResult with File object to save to file
0749:                                        StreamResult result = new StreamResult(
0750:                                                FileUtil.toFile(tomcatUsers));
0751:                                        DOMSource source = new DOMSource(
0752:                                                document);
0753:                                        transformer.transform(source, result);
0754:                                    }
0755:                                }
0756:                            }
0757:                        } catch (Exception ex) {
0758:                            Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO,
0759:                                    null, ex);
0760:                        }
0761:                    }
0762:                    return;
0763:                }
0764:
0765:                String dstPasswd = getDefaultPassword(project);
0766:                if (glassfish) {
0767:                    try {
0768:                        if (!client && refreshScript)
0769:                            refreshBuildScript(project);
0770:                        String serverKeyStorePath = getStoreLocation(project,
0771:                                false, false);
0772:                        String serverTrustStorePath = getStoreLocation(project,
0773:                                true, false);
0774:                        String clientKeyStorePath = getStoreLocation(project,
0775:                                false, true);
0776:                        String clientTrustStorePath = getStoreLocation(project,
0777:                                true, true);
0778:                        copyKey(SERVER_KEYSTORE_BUNDLED, XWS_SECURITY_SERVER,
0779:                                PASSWORD, PASSWORD, serverKeyStorePath,
0780:                                XWS_SECURITY_SERVER, dstPasswd, false);
0781:                        copyKey(SERVER_KEYSTORE_BUNDLED, WSSIP, PASSWORD,
0782:                                PASSWORD, serverKeyStorePath, WSSIP, dstPasswd,
0783:                                false);
0784:                        copyKey(SERVER_TRUSTSTORE_BUNDLED,
0785:                                "certificate-authority", PASSWORD, PASSWORD,
0786:                                serverTrustStorePath,
0787:                                "xwss-certificate-authority", dstPasswd, true);
0788:                        copyKey(SERVER_TRUSTSTORE_BUNDLED, XWS_SECURITY_CLIENT,
0789:                                PASSWORD, PASSWORD, serverTrustStorePath,
0790:                                XWS_SECURITY_CLIENT, dstPasswd, true);
0791:                        copyKey(CLIENT_KEYSTORE_BUNDLED, XWS_SECURITY_CLIENT,
0792:                                PASSWORD, PASSWORD, clientKeyStorePath,
0793:                                XWS_SECURITY_CLIENT, dstPasswd, false);
0794:                        copyKey(CLIENT_TRUSTSTORE_BUNDLED, XWS_SECURITY_SERVER,
0795:                                PASSWORD, PASSWORD, clientTrustStorePath,
0796:                                XWS_SECURITY_SERVER, dstPasswd, true);
0797:                        copyKey(CLIENT_TRUSTSTORE_BUNDLED, WSSIP, PASSWORD,
0798:                                PASSWORD, clientTrustStorePath, WSSIP,
0799:                                dstPasswd, true);
0800:                    } catch (Exception ex) {
0801:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null,
0802:                                ex);
0803:                    }
0804:                }
0805:            }
0806:
0807:            public static void copyKey(String srcPath, String srcAlias,
0808:                    String srcPasswd, String srcKeyPasswd, String dstPath,
0809:                    String dstAlias, String dstPasswd, boolean trustedCertEntry)
0810:                    throws Exception {
0811:                KeyStore srcStore = KeyStore.getInstance("JKS");
0812:                KeyStore dstStore = KeyStore.getInstance("JKS");
0813:                srcStore.load(Util.class.getResourceAsStream(srcPath),
0814:                        srcPasswd.toCharArray());
0815:                InputStream is = new FileInputStream(dstPath);
0816:                try {
0817:                    dstStore.load(is, dstPasswd.toCharArray());
0818:                    Key privKey = srcStore.getKey(srcAlias, srcKeyPasswd
0819:                            .toCharArray());
0820:
0821:                    if (is != null)
0822:                        is.close();
0823:
0824:                    OutputStream os = new FileOutputStream(dstPath);
0825:                    try {
0826:                        if (privKey == null || trustedCertEntry) {
0827:                            //this is a cert-entry
0828:                            dstStore.setCertificateEntry(dstAlias, srcStore
0829:                                    .getCertificate(srcAlias));
0830:                        } else {
0831:                            Certificate cert = srcStore
0832:                                    .getCertificate(srcAlias);
0833:                            Certificate[] chain = new Certificate[] { cert };
0834:                            dstStore.setKeyEntry(dstAlias, privKey, dstPasswd
0835:                                    .toCharArray(), chain);
0836:                        }
0837:                        dstStore.store(os, dstPasswd.toCharArray());
0838:                    } finally {
0839:                        if (os != null)
0840:                            os.close();
0841:                    }
0842:                } finally {
0843:                    if (is != null)
0844:                        is.close();
0845:                }
0846:            }
0847:
0848:            public static final boolean isTomcat(Project project) {
0849:                String sID = getServerInstanceID(project);
0850:                if (sID != null) {
0851:                    if ((sID != null) && (sID.toLowerCase().contains("tomcat"))) { //NOI18N
0852:                        return true;
0853:                    }
0854:                }
0855:                return false;
0856:            }
0857:
0858:            public static final boolean isGlassfish(Project project) {
0859:                if (project != null) {
0860:                    J2eeModuleProvider mp = project.getLookup().lookup(
0861:                            J2eeModuleProvider.class);
0862:                    if (mp == null)
0863:                        return false;
0864:                    return isGlassfish(mp.getServerInstanceID());
0865:                }
0866:                return false;
0867:            }
0868:
0869:            public static final boolean isGlassfish(String serverID) {
0870:                if ((serverID != null)
0871:                        && (serverID.toLowerCase().contains("appserv"))) { //NOI18N
0872:                    return true;
0873:                }
0874:                return false;
0875:            }
0876:
0877:            public static final boolean isWebProject(Project project) {
0878:                if (getProjectType(project) == ProjectType.WEB) {
0879:                    return true;
0880:                }
0881:                return false;
0882:            }
0883:
0884:            public static final boolean isJsr109Project(Project project) {
0885:                if ((getProjectType(project) == ProjectType.WEB)
0886:                        || (getProjectType(project) == ProjectType.EJB)) {
0887:                    return true;
0888:                }
0889:                return false;
0890:            }
0891:
0892:            private static J2eeModuleProvider getProvider(Project p) {
0893:                if (p != null) {
0894:                    return p.getLookup().lookup(J2eeModuleProvider.class);
0895:                }
0896:                return null;
0897:            }
0898:
0899:            public static final ProjectType getProjectType(Project project) {
0900:                J2eeModuleProvider mp = getProvider(project);
0901:                if (mp != null) {
0902:                    J2eeModule jm = mp.getJ2eeModule();
0903:                    if (jm != null) {
0904:                        Object moduleType = jm.getModuleType();
0905:                        if (J2eeModule.WAR.equals(moduleType)) {
0906:                            return ProjectType.WEB;
0907:                        } else if (J2eeModule.EJB.equals(moduleType)) {
0908:                            return ProjectType.EJB;
0909:                        } else if (J2eeModule.CLIENT.equals(moduleType)) {
0910:                            return ProjectType.CLIENT;
0911:                        }
0912:                    }
0913:                }
0914:                return ProjectType.UNKNOWN;
0915:            }
0916:
0917:            private static boolean isOperationInList(String operName,
0918:                    Collection<BindingOperation> operations) {
0919:                Iterator<BindingOperation> i = operations.iterator();
0920:                while (i.hasNext()) {
0921:                    BindingOperation bo = i.next();
0922:                    if ((bo != null) && (operName.equals(bo.getName()))) {
0923:                        return true;
0924:                    }
0925:                }
0926:                return false;
0927:            }
0928:
0929:            public static Collection<BindingOperation> refreshOperations(
0930:                    Binding binding, FileObject jc) {
0931:
0932:                if (binding == null) {
0933:                    return null;
0934:                }
0935:
0936:                Collection<BindingOperation> operations = binding
0937:                        .getBindingOperations();
0938:                if (jc == null) {
0939:                    return operations;
0940:                }
0941:
0942:                PortType pt = getPortType(binding);
0943:
0944:                // create operations and add them to the binding element
0945:                List<String> bindingOperationNames = JavaWsdlMapper
0946:                        .getOperationNames(jc);
0947:                for (String name : bindingOperationNames) {
0948:                    if (!isOperationInList(name, operations)) {
0949:                        generateOperation(binding, pt, name, jc);
0950:                    }
0951:                }
0952:
0953:                return binding.getBindingOperations();
0954:            }
0955:
0956:            public static BindingOperation generateOperation(Binding binding,
0957:                    PortType portType, String operationName,
0958:                    FileObject implClass) {
0959:                WSDLModel model = binding.getModel();
0960:                WSDLComponentFactory wcf = model.getFactory();
0961:                Definitions d = (Definitions) binding.getParent();
0962:
0963:                BindingOperation bindingOperation;
0964:
0965:                boolean isTransaction = model.isIntransaction();
0966:                if (!isTransaction) {
0967:                    model.startTransaction();
0968:                }
0969:
0970:                try {
0971:                    bindingOperation = wcf.createBindingOperation();
0972:                    bindingOperation.setName(operationName);
0973:                    binding.addBindingOperation(bindingOperation);
0974:
0975:                    // add input/output messages
0976:                    org.netbeans.modules.xml.wsdl.model.Message inputMsg = wcf
0977:                            .createMessage();
0978:                    inputMsg.setName(operationName);
0979:                    d.addMessage(inputMsg);
0980:
0981:                    org.netbeans.modules.xml.wsdl.model.Message outMsg = wcf
0982:                            .createMessage();
0983:                    outMsg.setName(operationName + "Response"); //NOI18N
0984:                    d.addMessage(outMsg);
0985:
0986:                    org.netbeans.modules.xml.wsdl.model.RequestResponseOperation oper = wcf
0987:                            .createRequestResponseOperation();
0988:                    oper.setName(operationName);
0989:                    portType.addOperation(oper);
0990:
0991:                    org.netbeans.modules.xml.wsdl.model.Input input = wcf
0992:                            .createInput();
0993:                    oper.setInput(input);
0994:                    input.setMessage(input.createReferenceTo(inputMsg,
0995:                            org.netbeans.modules.xml.wsdl.model.Message.class));
0996:
0997:                    org.netbeans.modules.xml.wsdl.model.Output out = wcf
0998:                            .createOutput();
0999:                    oper.setOutput(out);
1000:                    out.setMessage(out.createReferenceTo(outMsg,
1001:                            org.netbeans.modules.xml.wsdl.model.Message.class));
1002:
1003:                    org.netbeans.modules.xml.wsdl.model.BindingOutput bindingOutput = wcf
1004:                            .createBindingOutput();
1005:                    bindingOperation.setBindingOutput(bindingOutput);
1006:                    org.netbeans.modules.xml.wsdl.model.BindingInput bindingInput = wcf
1007:                            .createBindingInput();
1008:                    bindingOperation.setBindingInput(bindingInput);
1009:
1010:                    //add faults
1011:                    List<String> operationFaults = JavaWsdlMapper
1012:                            .getOperationFaults(implClass, operationName);
1013:                    for (String fault : operationFaults) {
1014:                        org.netbeans.modules.xml.wsdl.model.BindingFault bindingFault = wcf
1015:                                .createBindingFault();
1016:                        bindingOperation.addBindingFault(bindingFault);
1017:                    }
1018:                } finally {
1019:                    if (!isTransaction) {
1020:                        model.endTransaction();
1021:                    }
1022:                }
1023:
1024:                return bindingOperation;
1025:            }
1026:
1027:            public static PortType getPortType(Binding binding) {
1028:                Definitions d = (Definitions) binding.getParent();
1029:
1030:                QName portTypeQName = binding.getType().getQName();
1031:                PortType portType = null;
1032:
1033:                Collection<PortType> portTypes = d.getPortTypes();
1034:                Iterator<PortType> i = portTypes.iterator();
1035:                while (i.hasNext()) {
1036:                    PortType pt = i.next();
1037:                    if (pt != null) {
1038:                        if (portTypeQName.getLocalPart().equals(pt.getName())) {
1039:                            portType = pt;
1040:                            break;
1041:                        }
1042:                    }
1043:                }
1044:                return portType;
1045:            }
1046:
1047:            public static FileObject getFOForModel(WSDLModel model) {
1048:                if (model == null)
1049:                    return null;
1050:                ModelSource ms = model.getModelSource();
1051:                return Utilities.getFileObject(ms);
1052:            }
1053:
1054:            private static final String BUILD_SCRIPT = "/build.xml"; //NOI18N
1055:            private static final String BACKUP_EXT = ".bak"; //NOI18N
1056:            private static final String IMPORT_WSIT_DEPLOY_XML = "<import file=\"nbproject/wsit-deploy.xml\"/>"; //NOI18N
1057:            private static final String IMPORT_TAG = "<import"; //NOI18N
1058:            private static final String WSIT_DEPLOY_XML_PATH = "nbproject/wsit-deploy.xml"; //NOI18N
1059:            private static final String WSIT_DEPLOY_XSL = "org/netbeans/modules/websvc/wsitconf/resources/wsit-deploy.xsl"; //NOI18N
1060:
1061:            public static void refreshBuildScript(Project p) {
1062:                String buildScript = FileUtil.toFile(p.getProjectDirectory())
1063:                        .getPath()
1064:                        + BUILD_SCRIPT;
1065:
1066:                BufferedReader reader = null;
1067:                BufferedWriter writer = null;
1068:                String line = null;
1069:                boolean added = false;
1070:
1071:                // First check to see if our import statement has already been added.
1072:                try {
1073:                    reader = new BufferedReader(new FileReader(buildScript));
1074:                    while ((line = reader.readLine()) != null) {
1075:                        if (line.indexOf(IMPORT_WSIT_DEPLOY_XML) != -1) {
1076:                            added = true;
1077:                            break;
1078:                        }
1079:                    }
1080:                } catch (FileNotFoundException ex) {
1081:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null, ex);
1082:                } catch (IOException ex) {
1083:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null, ex);
1084:                } finally {
1085:                    if (reader != null) {
1086:                        try {
1087:                            reader.close();
1088:                        } catch (IOException ex) {
1089:                            Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO,
1090:                                    null, ex);
1091:                        }
1092:                    }
1093:                }
1094:
1095:                // If our import statement has not been added, add it now.
1096:                if (!added) {
1097:                    try {
1098:                        // Rename the original to build.xml.bak
1099:                        File backupBuildScript = new File(buildScript);
1100:                        backupBuildScript.renameTo(new File(buildScript
1101:                                + BACKUP_EXT));
1102:
1103:                        reader = new BufferedReader(new FileReader(buildScript
1104:                                + BACKUP_EXT));
1105:                        writer = new BufferedWriter(new FileWriter(buildScript));
1106:                        added = false;
1107:                        int index = 0;
1108:
1109:                        while ((line = reader.readLine()) != null) {
1110:                            if (!added
1111:                                    && (index = line.indexOf(IMPORT_TAG)) != -1) {
1112:                                StringBuffer buf = new StringBuffer(line);
1113:                                buf = buf.replace(index, line.length(),
1114:                                        IMPORT_WSIT_DEPLOY_XML);
1115:                                writer.write(buf.toString());
1116:                                writer.newLine();
1117:                                added = true;
1118:                            }
1119:
1120:                            writer.write(line);
1121:                            writer.newLine();
1122:                        }
1123:                    } catch (FileNotFoundException ex) {
1124:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null,
1125:                                ex);
1126:                    } catch (IOException ex) {
1127:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null,
1128:                                ex);
1129:                    } finally {
1130:                        try {
1131:                            if (writer != null) {
1132:                                writer.flush();
1133:                                writer.close();
1134:                            }
1135:
1136:                            if (reader != null) {
1137:                                reader.close();
1138:                            }
1139:                        } catch (IOException ex) {
1140:                            Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO,
1141:                                    null, ex);
1142:                        }
1143:                    }
1144:                }
1145:
1146:                // Now refresh the wsit-deploy.xml itself.
1147:                GeneratedFilesHelper genFilesHelper = new GeneratedFilesHelper(
1148:                        p.getProjectDirectory());
1149:
1150:                try {
1151:                    genFilesHelper.refreshBuildScript(WSIT_DEPLOY_XML_PATH,
1152:                            Util.class.getClassLoader().getResource(
1153:                                    WSIT_DEPLOY_XSL), false);
1154:                } catch (IOException ex) {
1155:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null, ex);
1156:                }
1157:            }
1158:
1159:            public static void unfillDefaults(Project p) {
1160:                String buildScript = FileUtil.toFile(p.getProjectDirectory())
1161:                        .getPath()
1162:                        + BUILD_SCRIPT;
1163:
1164:                FileObject createUserFile = p.getProjectDirectory()
1165:                        .getFileObject("nbproject/wsit.createuser");
1166:                if ((createUserFile != null) && (createUserFile.isValid())
1167:                        && !(createUserFile.isVirtual())) {
1168:                    try {
1169:                        createUserFile.delete();
1170:                    } catch (IOException ex) {
1171:                        Exceptions.printStackTrace(ex);
1172:                    }
1173:                }
1174:
1175:                BufferedReader reader = null;
1176:                BufferedWriter writer = null;
1177:                String line = null;
1178:                boolean added = false;
1179:
1180:                // First check to see if our import statement has already been added.
1181:                try {
1182:                    reader = new BufferedReader(new FileReader(buildScript));
1183:                    while ((line = reader.readLine()) != null) {
1184:                        if (line.indexOf(IMPORT_WSIT_DEPLOY_XML) != -1) {
1185:                            added = true;
1186:                            break;
1187:                        }
1188:                    }
1189:                } catch (FileNotFoundException ex) {
1190:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null, ex);
1191:                } catch (IOException ex) {
1192:                    Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null, ex);
1193:                } finally {
1194:                    if (reader != null) {
1195:                        try {
1196:                            reader.close();
1197:                        } catch (IOException ex) {
1198:                            Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO,
1199:                                    null, ex);
1200:                        }
1201:                    }
1202:                }
1203:
1204:                // If our import statement has not been added, add it now.
1205:                if (added) {
1206:                    try {
1207:                        // Rename the original to build.xml.bak
1208:                        File backupBuildScript = new File(buildScript);
1209:                        backupBuildScript.renameTo(new File(buildScript
1210:                                + BACKUP_EXT));
1211:
1212:                        reader = new BufferedReader(new FileReader(buildScript
1213:                                + BACKUP_EXT));
1214:                        writer = new BufferedWriter(new FileWriter(buildScript));
1215:                        added = false;
1216:
1217:                        while ((line = reader.readLine()) != null) {
1218:                            if ((line.indexOf(IMPORT_WSIT_DEPLOY_XML)) == -1) {
1219:                                writer.write(line);
1220:                                writer.newLine();
1221:                            }
1222:                        }
1223:                    } catch (FileNotFoundException ex) {
1224:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null,
1225:                                ex);
1226:                    } catch (IOException ex) {
1227:                        Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO, null,
1228:                                ex);
1229:                    } finally {
1230:                        try {
1231:                            if (writer != null) {
1232:                                writer.flush();
1233:                                writer.close();
1234:                            }
1235:
1236:                            if (reader != null) {
1237:                                reader.close();
1238:                            }
1239:                        } catch (IOException ex) {
1240:                            Logger.getLogger(LOGGER_GLOBAL).log(Level.INFO,
1241:                                    null, ex);
1242:                        }
1243:                    }
1244:                }
1245:            }
1246:
1247:            public static FileObject getSunDDFO(Project p) {
1248:                J2eeModuleProvider provider = getProvider(p);
1249:                if (provider == null)
1250:                    return null;
1251:                FileObject[] fobjs = provider.getConfigurationFiles();
1252:                if (fobjs.length > 0) {
1253:                    return fobjs[0];
1254:                }
1255:                return null;
1256:            }
1257:
1258:            public static Servlet getServlet(WebApp wa, String className) {
1259:                Servlet[] servlets = wa.getServlet();
1260:                for (Servlet s : servlets) {
1261:                    if (className.equals(s.getServletClass())) {
1262:                        return s;
1263:                    }
1264:                }
1265:                return null;
1266:            }
1267:
1268:            public static String getDefaultPassword(Project p) {
1269:                String password = KeystorePanel.DEFAULT_PASSWORD;
1270:                if (isGlassfish(p)) {
1271:                    String storeLoc = getStoreLocation(p, false, false);
1272:                    if (!passwordOK(storeLoc, password)) {
1273:                        password = Util.getPassword(p);
1274:                        if (!passwordOK(storeLoc, password)) {
1275:                            password = "";
1276:                        }
1277:                    }
1278:                }
1279:                return password;
1280:            }
1281:
1282:            public static String getDefaultPassword(String serverID) {
1283:                String password = KeystorePanel.DEFAULT_PASSWORD;
1284:                if (isGlassfish(serverID)) {
1285:                    String storeLoc = getStoreLocation(serverID, false, false);
1286:                    if (!passwordOK(storeLoc, password)) {
1287:                        password = KeystorePanel.DEFAULT_PASSWORD2;
1288:                        if (!passwordOK(storeLoc, password)) {
1289:                            password = "";
1290:                        }
1291:                    }
1292:                }
1293:                return password;
1294:            }
1295:
1296:            private static boolean passwordOK(String storePath, String password) {
1297:                try {
1298:                    Util.getAliases(storePath, password.toCharArray(),
1299:                            KeystorePanel.JKS);
1300:                } catch (Exception ex) {
1301:                    return false;
1302:                }
1303:                return true;
1304:            }
1305:
1306:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.