Source Code Cross Referenced for WebActionProvider.java in  » IDE-Netbeans » web.core » org » netbeans » modules » web » project » 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.web.project 
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.web.project;
0043:
0044:        import java.io.IOException;
0045:        import java.io.File;
0046:        import java.util.ArrayList;
0047:        import java.util.Arrays;
0048:        import java.util.HashMap;
0049:        import java.util.Iterator;
0050:        import java.util.List;
0051:        import java.util.Map;
0052:        import java.util.Properties;
0053:        import java.util.Set;
0054:        import java.util.regex.Pattern;
0055:        import org.apache.tools.ant.module.api.support.ActionUtils;
0056:        import org.netbeans.api.java.classpath.GlobalPathRegistry;
0057:        import org.netbeans.api.java.source.SourceUtils;
0058:        import org.netbeans.api.project.FileOwnerQuery;
0059:        import org.netbeans.api.project.Project;
0060:        import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef;
0061:        import org.netbeans.modules.j2ee.dd.api.web.WebAppMetadata;
0062:        import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeApplicationProvider;
0063:        import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
0064:        import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction;
0065:        import org.netbeans.spi.project.ActionProvider;
0066:        import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
0067:        import org.openide.filesystems.FileObject;
0068:        import org.openide.filesystems.FileUtil;
0069:        import org.openide.loaders.DataObject;
0070:        import org.openide.util.Lookup;
0071:        import org.openide.util.NbBundle;
0072:        import org.netbeans.modules.j2ee.deployment.plugins.api.*;
0073:        import org.netbeans.api.debugger.*;
0074:        import org.netbeans.api.debugger.jpda.*;
0075:        import org.netbeans.api.java.project.JavaProjectConstants;
0076:        import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
0077:        import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
0078:        import org.netbeans.modules.web.project.ui.SetExecutionUriAction;
0079:        import org.netbeans.modules.web.project.parser.JspNameUtil;
0080:        import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
0081:        import org.netbeans.modules.j2ee.dd.api.web.WebApp;
0082:        import org.netbeans.modules.j2ee.dd.api.web.Servlet;
0083:        import org.openide.DialogDisplayer;
0084:        import org.openide.NotifyDescriptor;
0085:        import org.netbeans.modules.web.api.webmodule.WebModule;
0086:        import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
0087:        import java.util.HashSet;
0088:        import javax.lang.model.element.TypeElement;
0089:        import org.netbeans.api.fileinfo.NonRecursiveFolder;
0090:        import org.netbeans.modules.java.api.common.ant.UpdateHelper;
0091:        import org.netbeans.api.java.source.CompilationController;
0092:        import org.netbeans.api.java.source.JavaSource;
0093:        import org.netbeans.modules.web.api.webmodule.RequestParametersQuery;
0094:        import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
0095:        import org.netbeans.modules.web.jsps.parserapi.JspParserFactory;
0096:        import org.netbeans.modules.web.project.ui.ServletUriPanel;
0097:        import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
0098:        import org.netbeans.modules.websvc.api.client.WsCompileClientEditorSupport;
0099:        import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
0100:        import org.netbeans.modules.websvc.api.webservices.WsCompileEditorSupport;
0101:        import org.netbeans.spi.project.support.ant.AntProjectHelper;
0102:        import org.netbeans.spi.project.support.ant.PropertyUtils;
0103:        import org.openide.DialogDescriptor;
0104:        import org.openide.ErrorManager;
0105:        import org.openide.util.Exceptions;
0106:
0107:        /** Action provider of the Web project. This is the place where to do
0108:         * strange things to Web actions. E.g. compile-single.
0109:         */
0110:        class WebActionProvider implements  ActionProvider {
0111:
0112:            // property definitions
0113:            private static final String DIRECTORY_DEPLOYMENT_SUPPORTED = "directory.deployment.supported"; // NOI18N
0114:
0115:            // Definition of commands
0116:
0117:            private static final String COMMAND_COMPILE = "compile"; //NOI18N
0118:            private static final String COMMAND_VERIFY = "verify"; //NOI18N
0119:
0120:            // Commands available from Web project
0121:            private static final String[] supportedActions = { COMMAND_BUILD,
0122:                    COMMAND_CLEAN, COMMAND_REBUILD, COMMAND_COMPILE_SINGLE,
0123:                    COMMAND_RUN, COMMAND_RUN_SINGLE, COMMAND_DEBUG,
0124:                    WebProjectConstants.COMMAND_REDEPLOY, COMMAND_DEBUG_SINGLE,
0125:                    JavaProjectConstants.COMMAND_JAVADOC, COMMAND_TEST,
0126:                    COMMAND_TEST_SINGLE, COMMAND_DEBUG_TEST_SINGLE,
0127:                    JavaProjectConstants.COMMAND_DEBUG_FIX, COMMAND_COMPILE,
0128:                    COMMAND_VERIFY, COMMAND_DELETE, COMMAND_COPY, COMMAND_MOVE,
0129:                    COMMAND_RENAME };
0130:
0131:            // Project
0132:            WebProject project;
0133:
0134:            // Ant project helper of the project
0135:            private UpdateHelper updateHelper;
0136:
0137:            /** Map from commands to ant targets */
0138:            Map/*<String,String[]>*/commands;
0139:
0140:            public WebActionProvider(WebProject project,
0141:                    UpdateHelper updateHelper) {
0142:
0143:                commands = new HashMap();
0144:                commands.put(COMMAND_BUILD, new String[] { "dist" }); // NOI18N
0145:                commands.put(COMMAND_CLEAN, new String[] { "clean" }); // NOI18N
0146:                commands.put(COMMAND_REBUILD, new String[] { "clean", "dist" }); // NOI18N
0147:                // the target name is compile-single, except for JSPs, where it is compile-single-jsp
0148:                commands.put(COMMAND_COMPILE_SINGLE,
0149:                        new String[] { "compile-single" }); // NOI18N
0150:                commands.put(COMMAND_RUN, new String[] { "run" }); // NOI18N
0151:                // the target name is run, except for Java files with main method, where it is run-main
0152:                commands.put(COMMAND_RUN_SINGLE, new String[] { "run" }); // NOI18N
0153:                commands.put(WebProjectConstants.COMMAND_REDEPLOY,
0154:                        new String[] { "run-deploy" }); // NOI18N
0155:                commands.put(COMMAND_DEBUG, new String[] { "debug" }); // NOI18N
0156:                // the target name is debug, except for Java files with main method, where it is debug-single-main
0157:                commands.put(COMMAND_DEBUG_SINGLE, new String[] { "debug" }); // NOI18N
0158:                commands.put(JavaProjectConstants.COMMAND_JAVADOC,
0159:                        new String[] { "javadoc" }); // NOI18N
0160:                commands.put(COMMAND_TEST, new String[] { "test" }); // NOI18N
0161:                commands.put(COMMAND_TEST_SINGLE,
0162:                        new String[] { "test-single" }); // NOI18N
0163:                commands.put(COMMAND_DEBUG_TEST_SINGLE,
0164:                        new String[] { "debug-test" }); // NOI18N
0165:                commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX,
0166:                        new String[] { "debug-fix" }); // NOI18N
0167:                commands.put(COMMAND_COMPILE, new String[] { "compile" }); // NOI18N
0168:                commands.put(COMMAND_VERIFY, new String[] { "verify" }); // NOI18N
0169:
0170:                this .updateHelper = updateHelper;
0171:                this .project = project;
0172:            }
0173:
0174:            private FileObject findBuildXml() {
0175:                return project.getProjectDirectory().getFileObject(
0176:                        project.getBuildXmlName());
0177:            }
0178:
0179:            public String[] getSupportedActions() {
0180:                return supportedActions;
0181:            }
0182:
0183:            public void invokeAction(final String command, final Lookup context)
0184:                    throws IllegalArgumentException {
0185:                if (COMMAND_DELETE.equals(command)) {
0186:                    DefaultProjectOperations
0187:                            .performDefaultDeleteOperation(project);
0188:                    return;
0189:                }
0190:
0191:                if (COMMAND_COPY.equals(command)) {
0192:                    DefaultProjectOperations
0193:                            .performDefaultCopyOperation(project);
0194:                    return;
0195:                }
0196:
0197:                if (COMMAND_MOVE.equals(command)) {
0198:                    DefaultProjectOperations
0199:                            .performDefaultMoveOperation(project);
0200:                    return;
0201:                }
0202:
0203:                if (COMMAND_RENAME.equals(command)) {
0204:                    DefaultProjectOperations.performDefaultRenameOperation(
0205:                            project, null);
0206:                    return;
0207:                }
0208:
0209:                Runnable action = new Runnable() {
0210:                    public void run() {
0211:                        Properties p = new Properties();
0212:                        String[] targetNames;
0213:
0214:                        targetNames = getTargetNames(command, context, p);
0215:                        if (targetNames == null) {
0216:                            return;
0217:                        }
0218:                        if (targetNames.length == 0) {
0219:                            targetNames = null;
0220:                        }
0221:                        if (p.keySet().size() == 0) {
0222:                            p = null;
0223:                        }
0224:                        try {
0225:                            FileObject buildFo = findBuildXml();
0226:                            if (buildFo == null || !buildFo.isValid()) {
0227:                                //The build.xml was deleted after the isActionEnabled was called
0228:                                NotifyDescriptor nd = new NotifyDescriptor.Message(
0229:                                        NbBundle.getMessage(
0230:                                                WebActionProvider.class,
0231:                                                "LBL_No_Build_XML_Found"),
0232:                                        NotifyDescriptor.WARNING_MESSAGE);
0233:                                DialogDisplayer.getDefault().notify(nd);
0234:                            } else {
0235:                                ActionUtils.runTarget(buildFo, targetNames, p);
0236:                            }
0237:                        } catch (IOException e) {
0238:                            Exceptions.printStackTrace(e);
0239:                        }
0240:                    }
0241:                };
0242:
0243:                action.run();
0244:            }
0245:
0246:            /**
0247:             * @return array of targets or null to stop execution; can return empty array
0248:             */
0249:            String[] getTargetNames(String command, Lookup context, Properties p)
0250:                    throws IllegalArgumentException {
0251:                String[] targetNames = (String[]) commands.get(command);
0252:
0253:                // RUN-SINGLE
0254:                if (command.equals(COMMAND_RUN_SINGLE)) {
0255:                    setDirectoryDeploymentProperty(p);
0256:                    FileObject[] files = findTestSources(context, false);
0257:                    if (files != null) {
0258:                        targetNames = setupTestSingle(p, files);
0259:                    } else {
0260:                        if (!isSelectedServer()) {
0261:                            return null;
0262:                        }
0263:                        if (isDebugged()) {
0264:                            p.setProperty("is.debugged", "true");
0265:                        }
0266:                        // 51462 - if there's an ejb reference, but no j2ee app, run/deploy will not work
0267:                        if (isEjbRefAndNoJ2eeApp(project)) {
0268:                            NotifyDescriptor nd;
0269:                            nd = new NotifyDescriptor.Message(NbBundle
0270:                                    .getMessage(WebActionProvider.class,
0271:                                            "MSG_EjbRef"),
0272:                                    NotifyDescriptor.INFORMATION_MESSAGE);
0273:                            DialogDisplayer.getDefault().notify(nd);
0274:                            return null;
0275:                        }
0276:                        if (command
0277:                                .equals(WebProjectConstants.COMMAND_REDEPLOY)) {
0278:                            p.setProperty("forceRedeploy", "true"); //NOI18N
0279:                        } else {
0280:                            p.setProperty("forceRedeploy", "false"); //NOI18N
0281:                        }
0282:                        // run a JSP
0283:                        files = findJsps(context);
0284:                        if (files != null && files.length > 0) {
0285:                            // possibly compile the JSP, if we are not compiling all of them
0286:                            String raw = updateHelper.getAntProjectHelper()
0287:                                    .getStandardPropertyEvaluator()
0288:                                    .getProperty(
0289:                                            WebProjectProperties.COMPILE_JSPS);
0290:                            boolean compile = decodeBoolean(raw);
0291:                            if (!compile) {
0292:                                setAllPropertiesForSingleJSPCompilation(p,
0293:                                        files);
0294:                            }
0295:
0296:                            String requestParams = RequestParametersQuery
0297:                                    .getFileAndParameters(files[0]);
0298:                            if (requestParams != null) {
0299:                                p.setProperty("client.urlPart", requestParams); //NOI18N
0300:                            } else {
0301:                                return null;
0302:                            }
0303:                        } else {
0304:                            // run HTML file
0305:                            FileObject[] htmlFiles = findHtml(context);
0306:                            if ((htmlFiles != null) && (htmlFiles.length > 0)) {
0307:                                String url = "/"
0308:                                        + FileUtil.getRelativePath(WebModule
0309:                                                .getWebModule(htmlFiles[0])
0310:                                                .getDocumentBase(),
0311:                                                htmlFiles[0]); // NOI18N
0312:                                if (url != null) {
0313:                                    url = org.openide.util.Utilities
0314:                                            .replaceString(url, " ", "%20");
0315:                                    p.setProperty("client.urlPart", url); //NOI18N
0316:                                } else {
0317:                                    return null;
0318:                                }
0319:                            } else {
0320:                                // run Java
0321:                                FileObject[] javaFiles = findJavaSources(context);
0322:                                if ((javaFiles != null)
0323:                                        && (javaFiles.length > 0)) {
0324:                                    FileObject javaFile = javaFiles[0];
0325:                                    if (!SourceUtils.getMainClasses(javaFile)
0326:                                            .isEmpty()) {
0327:                                        // run Java with Main method
0328:                                        String clazz = FileUtil
0329:                                                .getRelativePath(
0330:                                                        getRoot(
0331:                                                                project
0332:                                                                        .getSourceRoots()
0333:                                                                        .getRoots(),
0334:                                                                javaFile),
0335:                                                        javaFile);
0336:                                        p.setProperty("javac.includes", clazz); // NOI18N
0337:                                        // Convert foo/FooTest.java -> foo.FooTest
0338:                                        if (clazz.endsWith(".java")) { // NOI18N
0339:                                            clazz = clazz.substring(0, clazz
0340:                                                    .length() - 5);
0341:                                        }
0342:                                        clazz = clazz.replace('/', '.');
0343:
0344:                                        p.setProperty("run.class", clazz); // NOI18N
0345:                                        targetNames = new String[] { "run-main" };
0346:                                    } else {
0347:                                        // run servlet
0348:                                        // PENDING - what about servlets with main method? servlet should take precedence
0349:                                        String executionUri = (String) javaFile
0350:                                                .getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI);
0351:                                        if (executionUri != null) {
0352:                                            p.setProperty("client.urlPart",
0353:                                                    executionUri); //NOI18N
0354:                                        } else {
0355:                                            WebModule webModule = WebModule
0356:                                                    .getWebModule(javaFile);
0357:                                            String[] urlPatterns = SetExecutionUriAction
0358:                                                    .getServletMappings(
0359:                                                            webModule, javaFile);
0360:                                            if (urlPatterns != null
0361:                                                    && urlPatterns.length > 0) {
0362:                                                ServletUriPanel uriPanel = new ServletUriPanel(
0363:                                                        urlPatterns, null, true);
0364:                                                DialogDescriptor desc = new DialogDescriptor(
0365:                                                        uriPanel,
0366:                                                        NbBundle
0367:                                                                .getMessage(
0368:                                                                        WebActionProvider.class,
0369:                                                                        "TTL_setServletExecutionUri"));
0370:                                                Object res = DialogDisplayer
0371:                                                        .getDefault().notify(
0372:                                                                desc);
0373:                                                if (res
0374:                                                        .equals(NotifyDescriptor.YES_OPTION)) {
0375:                                                    p
0376:                                                            .setProperty(
0377:                                                                    "client.urlPart",
0378:                                                                    uriPanel
0379:                                                                            .getServletUri()); //NOI18N
0380:                                                    try {
0381:                                                        javaFile
0382:                                                                .setAttribute(
0383:                                                                        SetExecutionUriAction.ATTR_EXECUTION_URI,
0384:                                                                        uriPanel
0385:                                                                                .getServletUri());
0386:                                                    } catch (IOException ex) {
0387:                                                    }
0388:                                                } else
0389:                                                    return null;
0390:                                            } else {
0391:                                                String mes = java.text.MessageFormat
0392:                                                        .format(
0393:                                                                NbBundle
0394:                                                                        .getMessage(
0395:                                                                                WebActionProvider.class,
0396:                                                                                "TXT_noExecutableClass"),
0397:                                                                new Object[] { javaFile
0398:                                                                        .getName() });
0399:                                                NotifyDescriptor desc = new NotifyDescriptor.Message(
0400:                                                        mes,
0401:                                                        NotifyDescriptor.Message.ERROR_MESSAGE);
0402:                                                DialogDisplayer.getDefault()
0403:                                                        .notify(desc);
0404:                                                return null;
0405:                                            }
0406:                                        }
0407:                                    }
0408:                                }
0409:                            }
0410:                        }
0411:                    }
0412:
0413:                    // RUN, REDEPLOY
0414:                } else if (command.equals(COMMAND_RUN)
0415:                        || command.equals(WebProjectConstants.COMMAND_REDEPLOY)) {
0416:                    setDirectoryDeploymentProperty(p);
0417:                    FileObject[] files = findTestSources(context, false);
0418:                    if (files != null) {
0419:                        targetNames = setupTestSingle(p, files);
0420:                    } else {
0421:                        if (!isSelectedServer()) {
0422:                            return null;
0423:                        }
0424:                        if (isDebugged()) {
0425:                            p.setProperty("is.debugged", "true");
0426:                        }
0427:                        // 51462 - if there's an ejb reference, but no j2ee app, run/deploy will not work
0428:                        if (isEjbRefAndNoJ2eeApp(project)) {
0429:                            NotifyDescriptor nd;
0430:                            nd = new NotifyDescriptor.Message(NbBundle
0431:                                    .getMessage(WebActionProvider.class,
0432:                                            "MSG_EjbRef"),
0433:                                    NotifyDescriptor.INFORMATION_MESSAGE);
0434:                            DialogDisplayer.getDefault().notify(nd);
0435:                            return null;
0436:                        }
0437:                        if (command
0438:                                .equals(WebProjectConstants.COMMAND_REDEPLOY)) {
0439:                            p.setProperty("forceRedeploy", "true"); //NOI18N
0440:                        } else {
0441:                            p.setProperty("forceRedeploy", "false"); //NOI18N
0442:                        }
0443:                    }
0444:
0445:                    // DEBUG-SINGLE
0446:                } else if (command.equals(COMMAND_DEBUG_SINGLE)) {
0447:                    setDirectoryDeploymentProperty(p);
0448:                    FileObject[] files = findTestSources(context, false);
0449:                    if (files != null) {
0450:                        targetNames = setupDebugTestSingle(p, files);
0451:                    } else {
0452:                        if (!isSelectedServer()) {
0453:                            return null;
0454:                        }
0455:                        if (isDebugged()) {
0456:                            p.setProperty("is.debugged", "true");
0457:                        }
0458:                        // 51462 - if there's an ejb reference, but no j2ee app, debug will not work
0459:                        if (isEjbRefAndNoJ2eeApp(project)) {
0460:                            NotifyDescriptor nd;
0461:                            nd = new NotifyDescriptor.Message(NbBundle
0462:                                    .getMessage(WebActionProvider.class,
0463:                                            "MSG_EjbRef"),
0464:                                    NotifyDescriptor.INFORMATION_MESSAGE);
0465:                            DialogDisplayer.getDefault().notify(nd);
0466:                            return null;
0467:                        }
0468:
0469:                        files = findJsps(context);
0470:                        if ((files != null) && (files.length > 0)) {
0471:                            // debug jsp
0472:                            // possibly compile the JSP, if we are not compiling all of them
0473:                            String raw = updateHelper.getAntProjectHelper()
0474:                                    .getStandardPropertyEvaluator()
0475:                                    .getProperty(
0476:                                            WebProjectProperties.COMPILE_JSPS);
0477:                            boolean compile = decodeBoolean(raw);
0478:                            if (!compile) {
0479:                                setAllPropertiesForSingleJSPCompilation(p,
0480:                                        files);
0481:                            }
0482:
0483:                            String requestParams = RequestParametersQuery
0484:                                    .getFileAndParameters(files[0]);
0485:                            if (requestParams != null) {
0486:                                p.setProperty("client.urlPart", requestParams); //NOI18N
0487:                            } else {
0488:                                return null;
0489:                            }
0490:                        } else {
0491:                            // debug HTML file
0492:                            FileObject[] htmlFiles = findHtml(context);
0493:                            if ((htmlFiles != null) && (htmlFiles.length > 0)) {
0494:                                String url = "/"
0495:                                        + FileUtil.getRelativePath(WebModule
0496:                                                .getWebModule(htmlFiles[0])
0497:                                                .getDocumentBase(),
0498:                                                htmlFiles[0]); // NOI18N
0499:                                if (url != null) {
0500:                                    url = org.openide.util.Utilities
0501:                                            .replaceString(url, " ", "%20");
0502:                                    p.setProperty("client.urlPart", url); //NOI18N
0503:                                } else {
0504:                                    return null;
0505:                                }
0506:                            } else {
0507:                                // debug Java
0508:                                // debug servlet
0509:                                FileObject[] javaFiles = findJavaSources(context);
0510:                                if ((javaFiles != null)
0511:                                        && (javaFiles.length > 0)) {
0512:                                    FileObject javaFile = javaFiles[0];
0513:                                    if (!SourceUtils.getMainClasses(javaFile)
0514:                                            .isEmpty()) {
0515:                                        // debug Java with Main method
0516:                                        String clazz = FileUtil
0517:                                                .getRelativePath(
0518:                                                        getRoot(
0519:                                                                project
0520:                                                                        .getSourceRoots()
0521:                                                                        .getRoots(),
0522:                                                                javaFile),
0523:                                                        javaFile);
0524:                                        p.setProperty("javac.includes", clazz); // NOI18N
0525:                                        // Convert foo/FooTest.java -> foo.FooTest
0526:                                        if (clazz.endsWith(".java")) { // NOI18N
0527:                                            clazz = clazz.substring(0, clazz
0528:                                                    .length() - 5);
0529:                                        }
0530:                                        clazz = clazz.replace('/', '.');
0531:
0532:                                        p.setProperty("debug.class", clazz); // NOI18N
0533:                                        targetNames = new String[] { "debug-single-main" };
0534:                                    } else {
0535:                                        // run servlet
0536:                                        // PENDING - what about servlets with main method? servlet should take precedence
0537:                                        String executionUri = (String) javaFile
0538:                                                .getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI);
0539:                                        if (executionUri != null) {
0540:                                            p.setProperty("client.urlPart",
0541:                                                    executionUri); //NOI18N
0542:                                        } else {
0543:                                            WebModule webModule = WebModule
0544:                                                    .getWebModule(javaFile);
0545:                                            String[] urlPatterns = SetExecutionUriAction
0546:                                                    .getServletMappings(
0547:                                                            webModule, javaFile);
0548:                                            if (urlPatterns != null
0549:                                                    && urlPatterns.length > 0) {
0550:                                                ServletUriPanel uriPanel = new ServletUriPanel(
0551:                                                        urlPatterns, null, true);
0552:                                                DialogDescriptor desc = new DialogDescriptor(
0553:                                                        uriPanel,
0554:                                                        NbBundle
0555:                                                                .getMessage(
0556:                                                                        WebActionProvider.class,
0557:                                                                        "TTL_setServletExecutionUri"));
0558:                                                Object res = DialogDisplayer
0559:                                                        .getDefault().notify(
0560:                                                                desc);
0561:                                                if (res
0562:                                                        .equals(NotifyDescriptor.YES_OPTION)) {
0563:                                                    p
0564:                                                            .setProperty(
0565:                                                                    "client.urlPart",
0566:                                                                    uriPanel
0567:                                                                            .getServletUri()); //NOI18N
0568:                                                    try {
0569:                                                        javaFile
0570:                                                                .setAttribute(
0571:                                                                        SetExecutionUriAction.ATTR_EXECUTION_URI,
0572:                                                                        uriPanel
0573:                                                                                .getServletUri());
0574:                                                    } catch (IOException ex) {
0575:                                                    }
0576:                                                } else
0577:                                                    return null;
0578:                                            } else {
0579:                                                String mes = java.text.MessageFormat
0580:                                                        .format(
0581:                                                                NbBundle
0582:                                                                        .getMessage(
0583:                                                                                WebActionProvider.class,
0584:                                                                                "TXT_missingServletMappings"),
0585:                                                                new Object[] { javaFile
0586:                                                                        .getName() });
0587:                                                NotifyDescriptor desc = new NotifyDescriptor.Message(
0588:                                                        mes,
0589:                                                        NotifyDescriptor.Message.ERROR_MESSAGE);
0590:                                                DialogDisplayer.getDefault()
0591:                                                        .notify(desc);
0592:                                                return null;
0593:                                            }
0594:                                        }
0595:                                    }
0596:                                }
0597:                            }
0598:                        }
0599:                    }
0600:
0601:                    //DEBUG
0602:                } else if (command.equals(COMMAND_DEBUG)) {
0603:                    setDirectoryDeploymentProperty(p);
0604:                    if (!isSelectedServer()) {
0605:                        return null;
0606:                    }
0607:                    if (isDebugged()) {
0608:                        p.setProperty("is.debugged", "true");
0609:                    }
0610:                    // 51462 - if there's an ejb reference, but no j2ee app, debug will not work
0611:                    if (isEjbRefAndNoJ2eeApp(project)) {
0612:                        NotifyDescriptor nd;
0613:                        nd = new NotifyDescriptor.Message(NbBundle.getMessage(
0614:                                WebActionProvider.class, "MSG_EjbRef"),
0615:                                NotifyDescriptor.INFORMATION_MESSAGE);
0616:                        DialogDisplayer.getDefault().notify(nd);
0617:                        return null;
0618:                    }
0619:
0620:                    WebServicesClientSupport wscs = WebServicesClientSupport
0621:                            .getWebServicesClientSupport(project
0622:                                    .getProjectDirectory());
0623:                    if (wscs != null) { //project contains ws reference
0624:                        List serviceClients = wscs.getServiceClients();
0625:                        //we store all ws client names into hash set for later fast searching
0626:                        HashSet scNames = new HashSet();
0627:                        for (Iterator scIt = serviceClients.iterator(); scIt
0628:                                .hasNext();) {
0629:                            WsCompileClientEditorSupport.ServiceSettings serviceClientSettings = (WsCompileClientEditorSupport.ServiceSettings) scIt
0630:                                    .next();
0631:                            scNames.add(serviceClientSettings.getServiceName());
0632:                        }
0633:
0634:                        StringBuffer clientDCP = new StringBuffer();//additional debug.classpath
0635:                        StringBuffer clientWDD = new StringBuffer();//additional web.docbase.dir
0636:
0637:                        //we find all projects containg a web service            
0638:                        Set globalPath = GlobalPathRegistry.getDefault()
0639:                                .getSourceRoots();
0640:                        HashSet serverNames = new HashSet();
0641:                        //iteration through all source roots
0642:                        for (Iterator iter = globalPath.iterator(); iter
0643:                                .hasNext();) {
0644:                            FileObject sourceRoot = (FileObject) iter.next();
0645:                            Project serverProject = FileOwnerQuery
0646:                                    .getOwner(sourceRoot);
0647:                            if (serverProject != null) {
0648:                                if (!serverNames.add(serverProject
0649:                                        .getProjectDirectory().getName())) //project was already visited
0650:                                    continue;
0651:                                WebServicesSupport wss = WebServicesSupport
0652:                                        .getWebServicesSupport(serverProject
0653:                                                .getProjectDirectory());
0654:                                if (wss != null) { //project contains ws
0655:                                    List services = wss.getServices();
0656:                                    boolean match = false;
0657:                                    for (Iterator sIt = services.iterator(); sIt
0658:                                            .hasNext();) {
0659:                                        WsCompileEditorSupport.ServiceSettings serviceSettings = (WsCompileEditorSupport.ServiceSettings) sIt
0660:                                                .next();
0661:                                        String serviceName = serviceSettings
0662:                                                .getServiceName();
0663:                                        if (scNames.contains(serviceName)) { //matching ws name found
0664:                                            match = true;
0665:                                            break; //no need to continue
0666:                                        }
0667:                                    }
0668:                                    if (match) { //matching ws name found in project
0669:                                        //we need to add project's source folders onto a debugger's search path
0670:                                        AntProjectHelper serverHelper = wss
0671:                                                .getAntProjectHelper();
0672:                                        String dcp = serverHelper
0673:                                                .getStandardPropertyEvaluator()
0674:                                                .getProperty(
0675:                                                        WebProjectProperties.DEBUG_CLASSPATH);
0676:                                        if (dcp != null) {
0677:                                            String[] pathTokens = PropertyUtils
0678:                                                    .tokenizePath(dcp);
0679:                                            for (int i = 0; i < pathTokens.length; i++) {
0680:                                                File f = new File(pathTokens[i]);
0681:                                                if (!f.isAbsolute())
0682:                                                    pathTokens[i] = serverProject
0683:                                                            .getProjectDirectory()
0684:                                                            .getPath()
0685:                                                            + "/"
0686:                                                            + pathTokens[i];
0687:                                                clientDCP.append(pathTokens[i]
0688:                                                        + ":");
0689:                                            }
0690:                                        }
0691:
0692:                                        String wdd = serverHelper
0693:                                                .getStandardPropertyEvaluator()
0694:                                                .getProperty(
0695:                                                        WebProjectProperties.WEB_DOCBASE_DIR);
0696:                                        if (wdd != null) {
0697:                                            String[] pathTokens = PropertyUtils
0698:                                                    .tokenizePath(wdd);
0699:                                            for (int i = 0; i < pathTokens.length; i++) {
0700:                                                File f = new File(pathTokens[i]);
0701:                                                if (!f.isAbsolute())
0702:                                                    pathTokens[i] = serverProject
0703:                                                            .getProjectDirectory()
0704:                                                            .getPath()
0705:                                                            + "/"
0706:                                                            + pathTokens[i];
0707:                                                clientWDD.append(pathTokens[i]
0708:                                                        + ":");
0709:                                            }
0710:                                        }
0711:                                    }
0712:                                }
0713:                            }
0714:                        }
0715:                        p.setProperty(WebProjectProperties.WS_DEBUG_CLASSPATHS,
0716:                                clientDCP.toString());
0717:                        p.setProperty(WebProjectProperties.WS_WEB_DOCBASE_DIRS,
0718:                                clientWDD.toString());
0719:                    }
0720:
0721:                } else if (command
0722:                        .equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) {
0723:                    setDirectoryDeploymentProperty(p);
0724:                    FileObject[] files = findJavaSources(context);
0725:                    String path = null;
0726:                    final String[] classes = { "" };
0727:                    if (files != null) {
0728:                        path = FileUtil.getRelativePath(getRoot(project
0729:                                .getSourceRoots().getRoots(), files[0]),
0730:                                files[0]);
0731:                        targetNames = new String[] { "debug-fix" }; // NOI18N
0732:                        JavaSource js = JavaSource.forFileObject(files[0]);
0733:                        if (js != null) {
0734:                            try {
0735:                                js
0736:                                        .runUserActionTask(
0737:                                                new org.netbeans.api.java.source.Task<CompilationController>() {
0738:                                                    public void run(
0739:                                                            CompilationController ci)
0740:                                                            throws Exception {
0741:                                                        if (ci
0742:                                                                .toPhase(
0743:                                                                        JavaSource.Phase.ELEMENTS_RESOLVED)
0744:                                                                .compareTo(
0745:                                                                        JavaSource.Phase.ELEMENTS_RESOLVED) < 0) {
0746:                                                            ErrorManager
0747:                                                                    .getDefault()
0748:                                                                    .log(
0749:                                                                            ErrorManager.WARNING,
0750:                                                                            "Unable to resolve "
0751:                                                                                    + ci
0752:                                                                                            .getFileObject()
0753:                                                                                    + " to phase "
0754:                                                                                    + JavaSource.Phase.RESOLVED
0755:                                                                                    + ", current phase = "
0756:                                                                                    + ci
0757:                                                                                            .getPhase()
0758:                                                                                    + "\nDiagnostics = "
0759:                                                                                    + ci
0760:                                                                                            .getDiagnostics()
0761:                                                                                    + "\nFree memory = "
0762:                                                                                    + Runtime
0763:                                                                                            .getRuntime()
0764:                                                                                            .freeMemory());
0765:                                                            return;
0766:                                                        }
0767:                                                        List<? extends TypeElement> types = ci
0768:                                                                .getTopLevelElements();
0769:                                                        if (types.size() > 0) {
0770:                                                            for (TypeElement type : types) {
0771:                                                                if (classes[0]
0772:                                                                        .length() > 0) {
0773:                                                                    classes[0] = classes[0]
0774:                                                                            + " "; // NOI18N
0775:                                                                }
0776:                                                                classes[0] = classes[0]
0777:                                                                        + type
0778:                                                                                .getQualifiedName()
0779:                                                                                .toString()
0780:                                                                                .replace(
0781:                                                                                        '.',
0782:                                                                                        '/')
0783:                                                                        + "*.class"; // NOI18N
0784:                                                            }
0785:                                                        }
0786:                                                    }
0787:                                                }, true);
0788:                            } catch (java.io.IOException ioex) {
0789:                                Exceptions.printStackTrace(ioex);
0790:                            }
0791:                        }
0792:                    } else {
0793:                        return null;
0794:                    }
0795:                    // Convert foo/FooTest.java -> foo/FooTest
0796:                    if (path.endsWith(".java")) { // NOI18N
0797:                        path = path.substring(0, path.length() - 5);
0798:                    }
0799:                    p.setProperty("fix.includes", path); // NOI18N
0800:                    p.setProperty("fix.classes", classes[0]); // NOI18N
0801:
0802:                    //COMPILATION PART
0803:                } else if (command.equals(COMMAND_COMPILE_SINGLE)) {
0804:                    FileObject[] sourceRoots = project.getSourceRoots()
0805:                            .getRoots();
0806:                    FileObject[] files = findJavaSourcesAndPackages(context,
0807:                            sourceRoots);
0808:                    boolean recursive = (context
0809:                            .lookup(NonRecursiveFolder.class) == null);
0810:                    if (files != null) {
0811:                        p.setProperty("javac.includes", ActionUtils
0812:                                .antIncludesList(files, getRoot(sourceRoots,
0813:                                        files[0]), recursive)); // NOI18N
0814:                    } else {
0815:                        FileObject[] testRoots = project.getTestSourceRoots()
0816:                                .getRoots();
0817:                        files = findJavaSourcesAndPackages(context, testRoots);
0818:                        if (files != null) {
0819:                            p.setProperty("javac.includes", ActionUtils
0820:                                    .antIncludesList(files, getRoot(testRoots,
0821:                                            files[0]), recursive)); // NOI18N
0822:                            targetNames = new String[] { "compile-test-single" }; // NOI18N
0823:                        } else {
0824:                            files = findJsps(context);
0825:                            if (files != null) {
0826:                                for (int i = 0; i < files.length; i++) {
0827:                                    FileObject jsp = files[i];
0828:                                    if (areIncludesModified(jsp)) {
0829:                                        invalidateClassFile(project, jsp);
0830:                                    }
0831:                                }
0832:                                setAllPropertiesForSingleJSPCompilation(p,
0833:                                        files);
0834:                                targetNames = new String[] { "compile-single-jsp" };
0835:                            } else {
0836:                                return null;
0837:                            }
0838:                        }
0839:                    }
0840:
0841:                    //TEST PART
0842:                } else if (command.equals(COMMAND_TEST_SINGLE)) {
0843:                    setDirectoryDeploymentProperty(p);
0844:                    FileObject[] files = findTestSourcesForSources(context);
0845:                    targetNames = setupTestSingle(p, files);
0846:                } else if (command.equals(COMMAND_DEBUG_TEST_SINGLE)) {
0847:                    setDirectoryDeploymentProperty(p);
0848:                    FileObject[] files = findTestSourcesForSources(context);
0849:                    targetNames = setupDebugTestSingle(p, files);
0850:                } else {
0851:                    if (targetNames == null) {
0852:                        throw new IllegalArgumentException(command);
0853:                    }
0854:                }
0855:                return targetNames;
0856:            }
0857:
0858:            private String[] setupTestSingle(Properties p, FileObject[] files) {
0859:                FileObject[] testSrcPath = project.getTestSourceRoots()
0860:                        .getRoots();
0861:                FileObject root = getRoot(testSrcPath, files[0]);
0862:                p.setProperty("test.includes", ActionUtils.antIncludesList(
0863:                        files, root)); // NOI18N
0864:                p.setProperty("javac.includes", ActionUtils.antIncludesList(
0865:                        files, root)); // NOI18N
0866:                return new String[] { "test-single" }; // NOI18N
0867:            }
0868:
0869:            private String[] setupDebugTestSingle(Properties p,
0870:                    FileObject[] files) {
0871:                FileObject[] testSrcPath = project.getTestSourceRoots()
0872:                        .getRoots();
0873:                FileObject root = getRoot(testSrcPath, files[0]);
0874:                String path = FileUtil.getRelativePath(root, files[0]);
0875:                // Convert foo/FooTest.java -> foo.FooTest
0876:                p.setProperty("test.class", path
0877:                        .substring(0, path.length() - 5).replace('/', '.')); // NOI18N
0878:                return new String[] { "debug-test" }; // NOI18N
0879:            }
0880:
0881:            /* Deletes translated class/java file to force recompilation of the page with all includes
0882:             */
0883:            public void invalidateClassFile(WebProject wp, FileObject jsp) {
0884:                String dir = updateHelper.getAntProjectHelper()
0885:                        .getStandardPropertyEvaluator().getProperty(
0886:                                WebProjectProperties.BUILD_GENERATED_DIR);
0887:                if (dir == null) {
0888:                    return;
0889:                }
0890:                dir = dir + "/src"; //NOI18N
0891:                WebModule wm = WebModule.getWebModule(jsp);
0892:                if (wm == null) {
0893:                    return;
0894:                }
0895:                String name = JspNameUtil.getServletName(wm.getDocumentBase(),
0896:                        jsp);
0897:                if (name == null) {
0898:                    return;
0899:                }
0900:                String filePath = name.substring(0, name.lastIndexOf('.'))
0901:                        .replace('.', '/');
0902:
0903:                String fileClass = dir + '/' + filePath + ".class"; //NOI18N
0904:                String fileJava = dir + '/' + filePath + ".java"; //NOI18N
0905:
0906:                FileObject fC = FileUtil.toFileObject(updateHelper
0907:                        .getAntProjectHelper().resolveFile(fileClass));
0908:                FileObject fJ = FileUtil.toFileObject(updateHelper
0909:                        .getAntProjectHelper().resolveFile(fileJava));
0910:                try {
0911:                    if ((fJ != null) && (fJ.isValid())) {
0912:                        fJ.delete();
0913:                    }
0914:                    if ((fC != null) && (fC.isValid())) {
0915:                        fC.delete();
0916:                    }
0917:                } catch (IOException e) {
0918:                    Exceptions.printStackTrace(e);
0919:                }
0920:            }
0921:
0922:            /* checks if timestamp of any of the included pages is higher than the top page
0923:             */
0924:            public boolean areIncludesModified(FileObject jsp) {
0925:                boolean modified = false;
0926:                WebModule wm = WebModule.getWebModule(jsp);
0927:                JspParserAPI jspParser = JspParserFactory.getJspParser();
0928:                JspParserAPI.ParseResult result = jspParser.analyzePage(jsp,
0929:                        wm, JspParserAPI.ERROR_IGNORE);
0930:                if (!result.isParsingSuccess()) {
0931:                    modified = true;
0932:                } else {
0933:                    List includes = result.getPageInfo().getDependants();
0934:                    if ((includes != null) && (includes.size() > 0)) {
0935:                        long jspTS = jsp.lastModified().getTime();
0936:                        int size = includes.size();
0937:                        for (int i = 0; i < size; i++) {
0938:                            String filename = (String) includes.get(i);
0939:                            filename = FileUtil.toFile(wm.getDocumentBase())
0940:                                    .getPath()
0941:                                    + filename;
0942:                            File f = new File(filename);
0943:                            long incTS = f.lastModified();
0944:                            if (incTS > jspTS) {
0945:                                modified = true;
0946:                                break;
0947:                            }
0948:                        }
0949:                    }
0950:                }
0951:                return modified;
0952:            }
0953:
0954:            // PENDING - should not this be in some kind of an API?
0955:            private boolean decodeBoolean(String raw) {
0956:                if (raw != null) {
0957:                    String lowecaseRaw = raw.toLowerCase();
0958:
0959:                    if (lowecaseRaw.equals("true") || // NOI18N
0960:                            lowecaseRaw.equals("yes") || // NOI18N
0961:                            lowecaseRaw.equals("enabled")) // NOI18N
0962:                        return true;
0963:                }
0964:
0965:                return false;
0966:            }
0967:
0968:            private void setAllPropertiesForSingleJSPCompilation(Properties p,
0969:                    FileObject[] files) {
0970:                p
0971:                        .setProperty("jsp.includes",
0972:                                getBuiltJspFileNamesAsPath(files)); // NOI18N
0973:                /*ActionUtils.antIncludesList(files, project.getWebModule ().getDocumentBase ())*/
0974:
0975:                p.setProperty("javac.jsp.includes",
0976:                        getCommaSeparatedGeneratedJavaFiles(files)); // NOI18N
0977:
0978:            }
0979:
0980:            public String getCommaSeparatedGeneratedJavaFiles(
0981:                    FileObject[] jspFiles) {
0982:                StringBuffer b = new StringBuffer();
0983:                for (int i = 0; i < jspFiles.length; i++) {
0984:                    String jspRes = getJspResource(jspFiles[i]);
0985:                    if (i > 0) {
0986:                        b.append(',');
0987:                    }
0988:                    b.append(Utils.getGeneratedJavaResource(jspRes));
0989:                }
0990:                return b.toString();
0991:            }
0992:
0993:            /** Returns a resource name for a given JSP separated by / (does not start with a /).
0994:             */
0995:            private String getJspResource(FileObject jsp) {
0996:                ProjectWebModule pwm = project.getWebModule();
0997:                FileObject webDir = pwm.getDocumentBase();
0998:                return FileUtil.getRelativePath(webDir, jsp);
0999:            }
1000:
1001:            public File getBuiltJsp(FileObject jsp) {
1002:                ProjectWebModule pwm = project.getWebModule();
1003:                FileObject webDir = pwm.getDocumentBase();
1004:                String relFile = FileUtil.getRelativePath(webDir, jsp).replace(
1005:                        '/', File.separatorChar);
1006:                File webBuildDir = pwm.getContentDirectoryAsFile();
1007:                return new File(webBuildDir, relFile);
1008:            }
1009:
1010:            public String getBuiltJspFileNamesAsPath(FileObject[] files) {
1011:                StringBuffer b = new StringBuffer();
1012:                for (int i = 0; i < files.length; i++) {
1013:                    String path = getBuiltJsp(files[i]).getAbsolutePath();
1014:                    if (i > 0) {
1015:                        b.append(File.pathSeparator);
1016:                    }
1017:                    b.append(path);
1018:                }
1019:                return b.toString();
1020:            }
1021:
1022:            public boolean isActionEnabled(String command, Lookup context) {
1023:                FileObject buildXml = findBuildXml();
1024:                if (buildXml == null || !buildXml.isValid()) {
1025:                    return false;
1026:                }
1027:                if (command.equals(COMMAND_DEBUG_SINGLE)) {
1028:                    return findJavaSources(context) != null
1029:                            || findJsps(context) != null
1030:                            || findHtml(context) != null
1031:                            || findTestSources(context, false) != null;
1032:                } else if (command.equals(COMMAND_COMPILE_SINGLE)) {
1033:                    return findJavaSourcesAndPackages(context, project
1034:                            .getSourceRoots().getRoots()) != null
1035:                            || findJavaSourcesAndPackages(context, project
1036:                                    .getTestSourceRoots().getRoots()) != null
1037:                            || findJsps(context) != null;
1038:                } else if (command.equals(COMMAND_VERIFY)) {
1039:                    return project.getWebModule().hasVerifierSupport();
1040:                } else if (command.equals(COMMAND_RUN_SINGLE)) {
1041:                    // test for jsps
1042:                    FileObject files[] = findJsps(context);
1043:                    if (files != null && files.length > 0)
1044:                        return true;
1045:                    // test for html pages
1046:                    files = findHtml(context);
1047:                    if (files != null && files.length > 0)
1048:                        return true;
1049:                    // test for servlets
1050:                    FileObject[] javaFiles = findJavaSources(context);
1051:                    if (javaFiles != null && javaFiles.length > 0) {
1052:                        if (javaFiles[0]
1053:                                .getAttribute(SetExecutionUriAction.ATTR_EXECUTION_URI) != null)
1054:                            return true;
1055:                        else if (Boolean.TRUE
1056:                                .equals(javaFiles[0]
1057:                                        .getAttribute("org.netbeans.modules.web.IsServletFile"))) //NOI18N
1058:                            return true;
1059:                        else if (isDDServlet(context, javaFiles[0])) {
1060:                            try {
1061:                                javaFiles[0]
1062:                                        .setAttribute(
1063:                                                "org.netbeans.modules.web.IsServletFile",
1064:                                                Boolean.TRUE); //NOI18N
1065:                            } catch (IOException ex) {
1066:                            }
1067:                            return true;
1068:                        } else
1069:                            return true; /* because of java main classes, otherwise we would return false */
1070:                    }
1071:                    javaFiles = findTestSources(context, false);
1072:                    if ((javaFiles != null) && (javaFiles.length > 0)) {
1073:                        return true;
1074:                    }
1075:                    return false;
1076:                } else if (command.equals(COMMAND_TEST_SINGLE)) {
1077:                    return findTestSourcesForSources(context) != null;
1078:                } else if (command.equals(COMMAND_DEBUG_TEST_SINGLE)) {
1079:                    FileObject[] files = findTestSourcesForSources(context);
1080:                    return files != null && files.length == 1;
1081:                } else {
1082:                    // other actions are global
1083:                    return true;
1084:                }
1085:
1086:            }
1087:
1088:            // Private methods -----------------------------------------------------
1089:
1090:            private static final String SUBST = "Test.java"; // NOI18N
1091:
1092:            /*
1093:             * copied from ActionUtils and reworked so that it checks for mimeType of files, and DOES NOT include files with suffix 'suffix'
1094:             */
1095:            private static FileObject[] findSelectedFilesByMimeType(
1096:                    Lookup context, FileObject dir, String mimeType,
1097:                    String suffix, boolean strict) {
1098:                if (dir != null && !dir.isFolder()) {
1099:                    throw new IllegalArgumentException("Not a folder: " + dir); // NOI18N
1100:                }
1101:                List/*<FileObject>*/files = new ArrayList();
1102:                Iterator it = context.lookup(
1103:                        new Lookup.Template(DataObject.class)).allInstances()
1104:                        .iterator();
1105:                while (it.hasNext()) {
1106:                    DataObject d = (DataObject) it.next();
1107:                    FileObject f = d.getPrimaryFile();
1108:                    boolean matches = FileUtil.toFile(f) != null;
1109:                    if (dir != null) {
1110:                        matches &= (FileUtil.isParentOf(dir, f) || dir == f);
1111:                    }
1112:                    if (mimeType != null) {
1113:                        matches &= f.getMIMEType().equals(mimeType);
1114:                    }
1115:                    if (suffix != null) {
1116:                        matches &= !f.getNameExt().endsWith(suffix);
1117:                    }
1118:                    // Generally only files from one project will make sense.
1119:                    // Currently the action UI infrastructure (PlaceHolderAction)
1120:                    // checks for that itself. Should there be another check here?
1121:                    if (matches) {
1122:                        files.add(f);
1123:                    } else if (strict) {
1124:                        return null;
1125:                    }
1126:                }
1127:                if (files.isEmpty()) {
1128:                    return null;
1129:                }
1130:                return (FileObject[]) files
1131:                        .toArray(new FileObject[files.size()]);
1132:            }
1133:
1134:            private static final Pattern SRCDIRJAVA = Pattern
1135:                    .compile("\\.java$"); // NOI18N
1136:
1137:            /** Find selected java sources 
1138:             */
1139:            private FileObject[] findJavaSources(Lookup context) {
1140:                FileObject[] srcPath = project.getSourceRoots().getRoots();
1141:                for (int i = 0; i < srcPath.length; i++) {
1142:                    FileObject[] files = ActionUtils.findSelectedFiles(context,
1143:                            srcPath[i], ".java", true); // NOI18N
1144:                    if (files != null) {
1145:                        return files;
1146:                    }
1147:                }
1148:                return null;
1149:            }
1150:
1151:            private FileObject[] findJavaSourcesAndPackages(Lookup context,
1152:                    FileObject srcDir) {
1153:                if (srcDir != null) {
1154:                    FileObject[] files = ActionUtils.findSelectedFiles(context,
1155:                            srcDir, null, true); // NOI18N
1156:                    //Check if files are either packages of java files
1157:                    if (files != null) {
1158:                        for (int i = 0; i < files.length; i++) {
1159:                            if (!files[i].isFolder()
1160:                                    && !"java".equals(files[i].getExt())) {
1161:                                return null;
1162:                            }
1163:                        }
1164:                    }
1165:                    return files;
1166:                } else {
1167:                    return null;
1168:                }
1169:            }
1170:
1171:            private FileObject[] findJavaSourcesAndPackages(Lookup context,
1172:                    FileObject[] srcRoots) {
1173:                for (int i = 0; i < srcRoots.length; i++) {
1174:                    FileObject[] result = findJavaSourcesAndPackages(context,
1175:                            srcRoots[i]);
1176:                    if (result != null) {
1177:                        return result;
1178:                    }
1179:                }
1180:                return null;
1181:            }
1182:
1183:            private FileObject[] findHtml(Lookup context) {
1184:                FileObject webDir = project.getWebModule().getDocumentBase();
1185:                FileObject[] files = null;
1186:                if (webDir != null) {
1187:                    files = findSelectedFilesByMimeType(context, webDir,
1188:                            "text/html", null, true);
1189:                }
1190:                return files;
1191:            }
1192:
1193:            /** Find selected jsps
1194:             */
1195:            private FileObject[] findJsps(Lookup context) {
1196:                FileObject webDir = project.getWebModule().getDocumentBase();
1197:                FileObject[] files = null;
1198:                if (webDir != null) {
1199:                    files = findSelectedFilesByMimeType(context, webDir,
1200:                            "text/x-jsp", ".jspf", true);
1201:                }
1202:                return files;
1203:            }
1204:
1205:            /** Find either selected tests or tests which belong to selected source files
1206:             */
1207:            private FileObject[] findTestSources(Lookup context,
1208:                    boolean checkInSrcDir) {
1209:                //XXX: Ugly, should be rewritten
1210:                FileObject[] testSrcPath = project.getTestSourceRoots()
1211:                        .getRoots();
1212:                for (int i = 0; i < testSrcPath.length; i++) {
1213:                    FileObject[] files = ActionUtils.findSelectedFiles(context,
1214:                            testSrcPath[i], ".java", true); // NOI18N
1215:                    if (files != null) {
1216:                        return files;
1217:                    }
1218:                }
1219:                if (checkInSrcDir && testSrcPath.length > 0) {
1220:                    FileObject[] files = findSources(context);
1221:                    if (files != null) {
1222:                        //Try to find the test under the test roots
1223:                        FileObject srcRoot = getRoot(project.getSourceRoots()
1224:                                .getRoots(), files[0]);
1225:                        for (int i = 0; i < testSrcPath.length; i++) {
1226:                            FileObject[] files2 = ActionUtils.regexpMapFiles(
1227:                                    files, srcRoot, SRCDIRJAVA, testSrcPath[i],
1228:                                    SUBST, true);
1229:                            if (files2 != null) {
1230:                                return files2;
1231:                            }
1232:                        }
1233:                    }
1234:                }
1235:                return null;
1236:            }
1237:
1238:            private boolean isEjbRefAndNoJ2eeApp(Project p) {
1239:
1240:                WebModule wmod = WebModule
1241:                        .getWebModule(p.getProjectDirectory());
1242:                if (wmod != null) {
1243:                    boolean hasEjbLocalRefs = false;
1244:                    try {
1245:                        wmod
1246:                                .getMetadataModel()
1247:                                .runReadAction(
1248:                                        new MetadataModelAction<WebAppMetadata, Boolean>() {
1249:                                            public Boolean run(
1250:                                                    WebAppMetadata metadata) {
1251:                                                // return true if there is an ejb reference in this module
1252:                                                EjbLocalRef[] ejbLocalRefs = metadata
1253:                                                        .getRoot()
1254:                                                        .getEjbLocalRef();
1255:                                                return ejbLocalRefs != null
1256:                                                        && ejbLocalRefs.length > 0;
1257:                                            }
1258:                                        });
1259:                    } catch (IOException e) {
1260:                        // ignore
1261:                    }
1262:                    if (hasEjbLocalRefs && !isInJ2eeApp(p)) {
1263:                        return true;
1264:                    }
1265:                }
1266:                return false;
1267:            }
1268:
1269:            private boolean isInJ2eeApp(Project p) {
1270:                Set globalPath = GlobalPathRegistry.getDefault()
1271:                        .getSourceRoots();
1272:                Iterator iter = globalPath.iterator();
1273:                while (iter.hasNext()) {
1274:                    FileObject sourceRoot = (FileObject) iter.next();
1275:                    Project project = FileOwnerQuery.getOwner(sourceRoot);
1276:                    if (project != null) {
1277:                        Object j2eeApplicationProvider = project.getLookup()
1278:                                .lookup(J2eeApplicationProvider.class);
1279:                        if (j2eeApplicationProvider != null) { // == it is j2ee app
1280:                            J2eeApplicationProvider j2eeApp = (J2eeApplicationProvider) j2eeApplicationProvider;
1281:                            J2eeModuleProvider[] j2eeModules = j2eeApp
1282:                                    .getChildModuleProviders();
1283:                            if ((j2eeModules != null)
1284:                                    && (j2eeModules.length > 0)) { // == there are some modules in the j2ee app
1285:                                J2eeModuleProvider affectedPrjProvider = (J2eeModuleProvider) p
1286:                                        .getLookup().lookup(
1287:                                                J2eeModuleProvider.class);
1288:                                if (affectedPrjProvider != null) {
1289:                                    if (Arrays.asList(j2eeModules).contains(
1290:                                            affectedPrjProvider)) {
1291:                                        return true;
1292:                                    }
1293:                                }
1294:                            }
1295:                        }
1296:                    }
1297:                }
1298:                return false;
1299:            }
1300:
1301:            private boolean isDebugged() {
1302:                J2eeModuleProvider jmp = (J2eeModuleProvider) project
1303:                        .getLookup().lookup(J2eeModuleProvider.class);
1304:                Session[] sessions = DebuggerManager.getDebuggerManager()
1305:                        .getSessions();
1306:                ServerDebugInfo sdi = null;
1307:
1308:                if (sessions != null && sessions.length > 0) {
1309:                    sdi = jmp.getServerDebugInfo();
1310:                    if (sdi == null)
1311:                        return false;
1312:                }
1313:
1314:                if (sessions != null) {
1315:                    for (int i = 0; i < sessions.length; i++) {
1316:                        Session s = sessions[i];
1317:                        if (s != null) {
1318:                            Object o = s.lookupFirst(null,
1319:                                    AttachingDICookie.class);
1320:                            if (o != null) {
1321:                                AttachingDICookie attCookie = (AttachingDICookie) o;
1322:                                if (sdi.getTransport().equals(
1323:                                        ServerDebugInfo.TRANSPORT_SHMEM)) {
1324:                                    String shmem = attCookie
1325:                                            .getSharedMemoryName();
1326:                                    if (shmem == null)
1327:                                        continue;
1328:                                    if (shmem.equalsIgnoreCase(sdi
1329:                                            .getShmemName()))
1330:                                        return true;
1331:                                } else {
1332:                                    String hostname = attCookie.getHostName();
1333:                                    if (hostname == null)
1334:                                        continue;
1335:                                    if (hostname
1336:                                            .equalsIgnoreCase(sdi.getHost()))
1337:                                        if (attCookie.getPortNumber() == sdi
1338:                                                .getPort())
1339:                                            return true;
1340:                                }
1341:                            }
1342:                        }
1343:                    }
1344:                }
1345:                return false;
1346:            }
1347:
1348:            private boolean isSelectedServer() {
1349:                String instance = updateHelper.getAntProjectHelper()
1350:                        .getStandardPropertyEvaluator().getProperty(
1351:                                WebProjectProperties.J2EE_SERVER_INSTANCE);
1352:                if (instance != null) {
1353:                    J2eeModuleProvider jmp = (J2eeModuleProvider) project
1354:                            .getLookup().lookup(J2eeModuleProvider.class);
1355:                    String sdi = jmp.getServerInstanceID();
1356:                    if (sdi != null) {
1357:                        String id = Deployment.getDefault().getServerID(sdi);
1358:                        if (id != null) {
1359:                            return true;
1360:                        }
1361:                    }
1362:                }
1363:
1364:                // if there is some server instance of the type which was used
1365:                // previously do not ask and use it
1366:                String serverType = updateHelper.getAntProjectHelper()
1367:                        .getStandardPropertyEvaluator().getProperty(
1368:                                WebProjectProperties.J2EE_SERVER_TYPE);
1369:                if (serverType != null) {
1370:                    String[] servInstIDs = Deployment.getDefault()
1371:                            .getInstancesOfServer(serverType);
1372:                    if (servInstIDs.length > 0) {
1373:                        setServerInstance(servInstIDs[0]);
1374:                        return true;
1375:                    }
1376:                }
1377:
1378:                // no selected server => warning
1379:                String msg = NbBundle.getMessage(WebActionProvider.class,
1380:                        "MSG_No_Server_Selected"); //  NOI18N
1381:                DialogDisplayer.getDefault().notify(
1382:                        new NotifyDescriptor.Message(msg,
1383:                                NotifyDescriptor.WARNING_MESSAGE));
1384:                return false;
1385:            }
1386:
1387:            private void setServerInstance(String serverInstanceId) {
1388:                WebProjectProperties.setServerInstance(project, updateHelper,
1389:                        serverInstanceId);
1390:            }
1391:
1392:            private boolean isDDServlet(Lookup context, FileObject javaClass) {
1393:                //        FileObject webDir = project.getWebModule ().getDocumentBase ();
1394:                //        if (webDir==null) return false;
1395:                //        FileObject fo = webDir.getFileObject("WEB-INF/web.xml"); //NOI18N
1396:
1397:                FileObject webInfDir = project.getWebModule().getWebInf();
1398:                if (webInfDir == null)
1399:                    return false;
1400:                FileObject fo = webInfDir
1401:                        .getFileObject(ProjectWebModule.FILE_DD);
1402:                if (fo == null)
1403:                    return false;
1404:
1405:                String relPath = FileUtil.getRelativePath(getRoot(project
1406:                        .getSourceRoots().getRoots(), javaClass), javaClass);
1407:                // #117888
1408:                String className = relPath.replace('/', '.').replaceFirst(
1409:                        "\\.java$", ""); // is there a better way how to do it?
1410:                try {
1411:                    WebApp webApp = DDProvider.getDefault().getDDRoot(fo);
1412:                    Servlet servlet = (Servlet) webApp.findBeanByName(
1413:                            "Servlet", "ServletClass", className); //NOI18N
1414:                    if (servlet != null)
1415:                        return true;
1416:                    else
1417:                        return false;
1418:                } catch (IOException ex) {
1419:                    return false;
1420:                }
1421:            }
1422:
1423:            /** Find tests corresponding to selected sources.
1424:             */
1425:            private FileObject[] findTestSourcesForSources(Lookup context) {
1426:                FileObject[] sourceFiles = findSources(context);
1427:                if (sourceFiles == null) {
1428:                    return null;
1429:                }
1430:                FileObject[] testSrcPath = project.getTestSourceRoots()
1431:                        .getRoots();
1432:                if (testSrcPath.length == 0) {
1433:                    return null;
1434:                }
1435:                FileObject[] srcPath = project.getSourceRoots().getRoots();
1436:                FileObject srcDir = getRoot(srcPath, sourceFiles[0]);
1437:                for (int i = 0; i < testSrcPath.length; i++) {
1438:                    FileObject[] files2 = ActionUtils.regexpMapFiles(
1439:                            sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i],
1440:                            SUBST, true);
1441:                    if (files2 != null) {
1442:                        return files2;
1443:                    }
1444:                }
1445:                return null;
1446:            }
1447:
1448:            private FileObject getRoot(FileObject[] roots, FileObject file) {
1449:                FileObject srcDir = null;
1450:                for (int i = 0; i < roots.length; i++) {
1451:                    if (FileUtil.isParentOf(roots[i], file)
1452:                            || roots[i].equals(file)) {
1453:                        srcDir = roots[i];
1454:                        break;
1455:                    }
1456:                }
1457:                return srcDir;
1458:            }
1459:
1460:            /** Find selected sources, the sources has to be under single source root,
1461:             *  @param context the lookup in which files should be found
1462:             */
1463:            private FileObject[] findSources(Lookup context) {
1464:                FileObject[] srcPath = project.getSourceRoots().getRoots();
1465:                for (int i = 0; i < srcPath.length; i++) {
1466:                    FileObject[] files = ActionUtils.findSelectedFiles(context,
1467:                            srcPath[i], ".java", true); // NOI18N
1468:                    if (files != null) {
1469:                        return files;
1470:                    }
1471:                }
1472:                return null;
1473:            }
1474:
1475:            private void setDirectoryDeploymentProperty(Properties p) {
1476:                String instance = updateHelper.getAntProjectHelper()
1477:                        .getStandardPropertyEvaluator().getProperty(
1478:                                WebProjectProperties.J2EE_SERVER_INSTANCE);
1479:                if (instance != null) {
1480:                    J2eeModuleProvider jmp = project.getLookup().lookup(
1481:                            J2eeModuleProvider.class);
1482:                    String sdi = jmp.getServerInstanceID();
1483:                    J2eeModule mod = jmp.getJ2eeModule();
1484:                    if (sdi != null && mod != null) {
1485:                        boolean cFD = Deployment.getDefault().canFileDeploy(
1486:                                instance, mod);
1487:                        p.setProperty(DIRECTORY_DEPLOYMENT_SUPPORTED, "" + cFD); // NOI18N
1488:                    }
1489:                }
1490:            }
1491:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.