Setting Up an IntelliJ IDEA Project Setting Up an IntelliJ IDEA Project Overview / CRX / CRX 2.3 / How To / This section describes how to set up a Web application project using the IntelliJ IDEA development environment. Prerequisites In order to set up the IntelliJ IDEA development environment as described in this guide, the following prerequisites must be fulfilled: • IntelliJ IDEA 4.x or higher installed • Sun JVM 1.4.x or higher installed • CRX 1.0 or higher installed PROJECT SETUP Setting up the IntelliJ IDEA project requires a large number of steps. For clarity, these steps are separated into different sections, although they are part of one Wizard. Creating the Project To create the IntelliJ IDEA project, proceed as follows: 1. 2. 3. If CRX is running, stop it. Start the IntelliJ development environment. In the File menu, click New Project. The New Project window opens. Type the project name and where you want to store the source files. Click Next to continue. 4. Click the JDK you want to use for this project. In most cases, this is the JDK on which CRX runs. Click Next to continue. 5. For a simple project, click Create single-module project, and then click Next to continue. 6. Click Web Module, and then click Next to continue. This creates a Java project that is suited for a Web environment, such as a Java Web application. 7. Type the name and content root for the new Web module. By default, the content root is the project root. Click Next to continue. © 2012 Adobe Systems Incorporated. All rights reserved. Page 1 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project Setting Up the Servlet Engine To configure it, proceed as follows (note that the installation is continued from the previous section): 1. Click the Configure button, and then click Generic to add a servlet engine. 2. Click Attach Classes and add all the servlet engine's libraries and all JAR files found in the /lib directories of the CRX servlet engine. The default location is c:\program files\Day\CRX-1.0.1\server\lib, and the folder contains the folders "common," "container," and "server." Click OK, and then select the newly added servlet engine, and then click Next to continue. Setting Up the Web Application In the final set of steps, you specify your Web application details (note that the installation is continued from the previous section): 1. The next step defines where the web.xml file of the Web application is located. Click Next to accept the default location. 2. For Web projects, IntelliJ usually keeps the resource files (JSPs, HTML, CSS and so on) separate from Java source files and the libraries (WEB-INF/*). Only when it builds the project, the different file categories are assembled and exported to the servlet engine as a WAR file and into the runtime directory of the Web application. Accept the default setting and click Next. 3. The next step defines the location of the so-called "exploded directory." When a WAR file is deployed in a servlet engine, the servlet engine extracts the WAR file into a runtime directory from where the © 2012 Adobe Systems Incorporated. All rights reserved. Page 2 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project files are served. This is the exploded directory. Point its location to the application’s directory inside the servlet engine’s runtime directory. As the application name, type MyCRXApp, for the Servlet Engine runtime directory, type c:\program files\day\CRX-1.0.1\server\runtime\0\. For directory to create, type _mycrxapp. For the exploded directory location, type c:\program files\day\CRX-1.0.1\server\runtime \0\_mycrxapp (note the preceding underscore). Click Next to continue. 4. Click Next to accept the default location for where the Java files are stored. 5. Select where the compiled Java classes are located. IntelliJ IDEA automatically sets it to <explodeddirectory>/WEB-INF/classes. Click Next to accept the default, and then click Finish to end the SetupWizard. CONFIGURING THE PROJECT In IntelliJ IDEA, you can now see the standard project view, as follows: Now, you need to adjust a few project parameters, as follows: 1. In the File menu, click Settings, and then click Modules. 2. Click the J2EE Build Settings tab. Select the Create web module war file check box and set the location to the servlet engine’s /webapps folder. This makes it easier to later move the application to other application servers. Make sure you name the WAR file identically to the application folder © 2012 Adobe Systems Incorporated. All rights reserved. Page 3 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project you point the exploded directory to, minus the underscore. For the example, the WAR file is named mycrxapp.war and is located in the folder c:\program files\day\CRX-1.0.1\server\webapps\. 3. Click the Libraries (Classpath) tab. For a JCR-based Web application, you need to add the JCR API library to the Web application. Click the Edit button next to the Used project libraries area. The Configure Project Libraries window opens. 4. 5. Click Create Library. Type a name for the library (for example jcr-1.0-lib), and add the jcr-1.0.jar file found in the CRX Web application library folder (c:\program files\day\CRX-1.0.1\server\runtime\0\_crx\WEB-INF\lib\jcr-1.0.jar). You can also copy the jar file to a central location from where it can be re-used for any project without being dependent on the CRX installation at hand. 6. Click OK. The new library appears in the list. 7. Click OK again. You can see a new entry in the Used project libraries area, with a selected check box next to it. © 2012 Adobe Systems Incorporated. All rights reserved. Page 4 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project 8. lick the Web Module Settings tab and make sure that the newly added project library is packaged to the WEB-INF/lib folder when building the application. 9. Click OK to save the settings. This concludes the project configuration. DEBUGGING AND SERVLET ENGINE CONFIGURATION As an example, add a JSP (Java Server Pages) file, as follows: 1. Right-click the resources folder in the project view, point to New, and click Jsp. 2. As name, type index.jsp, and then click OK. IntelliJ IDEA creates a new JSP file, already containing a few of lines of code. Configuring Debugging Before working with Java classes, configure debugging. This makes it easier to develop and debug Java classes, and allows you to modify the Web application restarting it. Proceed as follows: 1. Click Edit Configurations. 2. Click the Remote tab and add an entry for our servlet engine. As name, type (for example) CQSE Remote. Leave all settings on default. Highlight the string under Use the following command line arguments… and copy it to the clipboard (press CTRL+C). Click OK to add the debug connection. © 2012 Adobe Systems Incorporated. All rights reserved. Page 5 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project 3. Open the Communiqué Manager, which controls the CRX servlet engine, as follows: In the Start menu, point to AllPrograms, point to Day CRX 1.4, and then click Start CRX 1.4. If CRX is on autostart, immediately stop it again. Then double-click the entry in the list of instances and paste the string from the IntelliJ Remote configuration into the beginning of the JVM Options field. Clear the autostart flag: 4. Click OK to accept the changes. Do not start CRX yet. Building the Project Return to the IntelliJ project to build the project. This compiles the Java source files and adds all resource files, classes and libraries into the WAR file, and extracts the WAR file to the servlet engine’s runtime directory (exploded directory). You have to do this at least once before you can add the Web application to the servlet engine’s configuration file and start CRX. 1. Click Make Project (or press CTRL+F9). 2. Open Windows Explorer. You can see that the Web application has been extracted to the runtime directory. 3. The WAR file has been created as well. Configuring the Web Application Edit the servlet engine’s configuration file and add the new Web application. The configuration file is located at c:\program files\Day\CRX-1.4\server\etc\server.xml. © 2012 Adobe Systems Incorporated. All rights reserved. Page 6 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project Add the Web application as follows: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE server PUBLIC "-//Day Software AG//DTD J2EE Server 3.0//EN" "http://www.day.com/dtds/ server_3_0.dtd"> <server> <servletengine> <admin> <crypt>adpexzg3FUZAk</crypt> </admin> <container> <listener> <bind-address>0.0.0.0</bind-address> <bind-port>7402</bind-port> <max-threads>128</max-threads> </listener> <web-app> <context>/crx</context> <path>webapps/crx.war</path> <run-on-startup>true</run-on-startup> </web-app> <web-app> <context>/mycrxapp</context> <path>webapps/mycrxapp.war</path> <run-on-startup>true</run-on-startup> </web-app> <log-file> <log-file-name>logs/access.log</log-file-name> </log-file> </container> </servletengine> <log-file> <log-file-name>logs/server.log</log-file-name> </log-file> <log-level>WARN</log-level> </server> Now start CRX using the Communiqué Manager. Check the files startup.log and server.log in c:\program files\Day\CRX-1.4\server\logs for errors. TESTING THE SETUP The following steps help you to test the CRX setup step-by-step. Server Availability Check If you followed this guide, you have created an example JSP file named index.jsp. To see the output of the file in a Web browser, type the following address in the browser’s Address field: http://localhost:7402/mycrxapp/index.jsp If you have configured everything correctly, you can see the following text in the browser: Repository Access Code The following check uses a Java class to connect to the CRX repository via JNDI (the Java Naming and Directory Interface, which is the default method to connect to a different Web application running on the same Java Virtual Machine). The following example creates a Java class that retrieves the repository by JNDI and creates a session using the admin user: © 2012 Adobe Systems Incorporated. All rights reserved. Page 7 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project 1. In the project view, right-click on the src folder, point to New, and then click Package. Type com.day.test and click OK. 2. Right-click the newly created package, point to New, and click Class. Type RepositorySessionHandler as its name and click OK. 3. Open the newly created Java source file and paste the following code into it: package com.day.test; import javax.jcr.*; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; public class RepositorySessionHandler { public static Session getSession() throws NamingException, RepositoryException, LoginException { Hashtable env = new Hashtable(); env.put("java.naming.provider.url", "http://jcr.day.com"); env.put("java.naming.factory.initial", "com.day.util.jndi.provider.MemoryInitialContextFactory"); InitialContext ctx = new InitialContext(env); Repository repository = (Repository) ctx.lookup("crx"); SimpleCredentials creds = new SimpleCredentials("admin", "admin".toCharArray()); return repository.login(creds, "default"); } } 4. Open the JSP file index.jsp and paste the following code: <%@ page import="com.day.test.RepositorySessionHandler"%> <%@ page import="javax.jcr.Session"%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% Session repSession = RepositorySessionHandler.getSession(); %> <html> <head><title>Simple jsp page</title></head> <body> Current Workspace: <%=repSession.getWorkspace().getName()%><br> </body> </html> © 2012 Adobe Systems Incorporated. All rights reserved. Page 8 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project Restarting the Servlet Engine The JSP code uses the RepositorySessionHandler to access the repository. Press CTRL+F9 to build the project. Because you have created a new Java class, you now need to restart the servlet engine. A restart is required in the following cases: • • • You create a new Java class. You delete a Java class. You modify the so-called "signature" of a class or its methods. The signature specifies how the class interacts with other classes. You add or change the project libraries. • If you have an active debug connection, you do not need to restart in the following cases. • • You modify Java program code in a class or method. You modify JSP code. Establishing the Debug Connection To start a debug connection in IntelliJ IDEA, proceed as follows: 1. 2. In the list of the servlet engines, click the name of the servlet engine that runs your code. Click the debug button (green bug icon). The Debug window opens. 3. Leave everything as is and click Debug to connect to the servlet engine. This opens the debugging pane in the project view 4. You can now use the debugging pane for development. © 2012 Adobe Systems Incorporated. All rights reserved. Page 9 Created on 2014-11-14 Setting Up an IntelliJ IDEA Project 5. If you change a class and re-build, IntelliJ IDEA asks you whether you want to reload the classes in the servlet engine. Click Yes to make the changes active on the servlet engine. Running the Repository Access Code If you have followed the example so far, type the following address in the Address field of your Web browser: http://localhost:7402/mycrxapp/index.jsp If the servlet engine and the repository are running correctly, you can see the following message: © 2012 Adobe Systems Incorporated. All rights reserved. Page 10 Created on 2014-11-14
© Copyright 2024