Skip to content

Commit 2416cbd

Browse files
authored
Merge pull request docker#50 from docker/java-debug
adding in container java debugging example
2 parents 0d6828c + 1840758 commit 2416cbd

File tree

161 files changed

+29319
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+29319
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
## In-container Java Development: Eclipse
2+
3+
### Pre-requisites
4+
5+
* [Docker for OSX or Docker for Windows](https://www.docker.com/products/docker)
6+
* [Eclipse](http://www.eclipse.org/downloads/) (install Eclipse IDE for Java EE Developers)
7+
* [Java Development Kit](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
8+
* [Maven for Eclipse](http://www.eclipse.org/m2e/) (see instructions for adding the Maven plug-in to Eclipse)
9+
10+
### Getting Started
11+
12+
In Eclipse, clone the [registration-docker](https://github.com/spara/registration-docker.git) repository
13+
14+
`File`> `Import`
15+
Select `Git`> `Projects`> `Next`
16+
17+
![](images/eclipse_git_import_repo2.png)
18+
19+
Select `Clone URI`> `Next`
20+
21+
![](images/eclipse_git_clone_uri2.png)
22+
23+
Enter the [`repositoryurl`](https://github.com/spara/registration-docker.git)> `Next`
24+
25+
![](images/eclipse_git_repo_uri2.png)
26+
27+
`Select the master branch`> `Next`
28+
29+
![](images/eclipse_git_select_branch2.png)
30+
31+
`Enter the destination directory`> `Next`
32+
33+
![](images/eclipse_git_local_destination2.png)
34+
35+
Select the import wizard, `Import existing Eclipse project`> `Next`
36+
37+
![](images/eclipse_git_import_wizard2.png)
38+
39+
Select `registration-docker`> `Finish`
40+
41+
![](images/eclipse_git_import_project2.png)
42+
43+
### Building the application
44+
45+
The application is a basic Spring MVC application that receives user input from a form, writes the data to a database, and queries the database.
46+
47+
The application is built using Maven. To build the application click on `Run` > `Run configurations`
48+
49+
![](images/eclipse_maven_run_config3.png)
50+
51+
Select `Maven build` > `New`
52+
53+
![](images/eclipse_maven_build_new.png)
54+
55+
Enter a `Name` for the configuration.
56+
57+
Set the base direct of the application `<path>/registration-docker/app`.
58+
59+
Set the `Goals` to `clean install`.
60+
61+
Click `Apply`
62+
63+
Click `Run`
64+
65+
![](images/eclipse_maven_run_config_apply.png)
66+
67+
The results of the build will be displayed in the console.
68+
69+
![](images/eclipse_maven_console_build_result.png)
70+
71+
### Running the application
72+
73+
Open a terminal and go to the application directory. Start the application with docker-compose
74+
75+
<pre>&gt; docker-compose up </pre>
76+
77+
Docker will build the images for Apache Tomcat and MySQL and start the containers. It will also mount the application directory (`./app/target/UserSignup`) as a data volume on the host system to the Tomcat webapps directory in the web server container.
78+
79+
Open a browser window and go to:
80+
'localhost:8080'; you should see the Tomcat home page
81+
82+
![](images/tomcat_home3.png)
83+
84+
When the Tomcat image was built, the user roles were also configured. Click on the `Manager App` button to see the deployed applications. When prompted for username and password, enter `system` and `manager` respectively to log into the Tomcat Web Application Manager page.
85+
86+
![](images/tomcat_web_application_manager3.png)
87+
88+
You can use the Manager page to `Start`, `Stop`, `Reload` or `Undeploy` web applications.
89+
90+
To go to the application, Click on `/UserSignup` link.
91+
92+
![](images/app_index_page3.png)
93+
94+
### Debugging the Application
95+
96+
In the application, click on `Signup` to create a new user. Fill out the registration form and click `Submit`
97+
98+
![](images/app_debug_signup2.png)
99+
100+
Click `Yes` to confirm.
101+
102+
![](images/app_debug_signup_confirm.png)
103+
104+
Test out the login.
105+
106+
![](images/app_debug_login2.png)
107+
108+
Oh no!
109+
110+
![](images/app_debug_login_fail2.png)
111+
112+
#### Configure Remote Debugging
113+
114+
Tomcat supports remote debugging the Java Platform Debugger Architecture (JPDA). Remote debugging was enabled when the tomcat image (registration-webserver) was built.
115+
116+
To configure remote debugging in Eclipse, click on `Run` > `Debug Configurations ...`
117+
118+
![](images/eclipse_debug_configure2.png)
119+
120+
Select `Remote Java Application` and click on `Launch New Configuration` icon
121+
122+
![](images/eclipse_debug_configure_new.png)
123+
124+
Enter a `Name` for the configuration. Select the project using the `browse` button. Click on `Apply` to save the configuration and click on `Debug` to start the debugging connection between Tomcat and Eclipse.
125+
126+
![](images/eclipse_debug_configure_docker.png)
127+
128+
#### Finding the Error
129+
130+
Since the problem is with the password, lets see how the password is set in the User class. In the User class, the setter for password is scrambled using [rot13](https://en.wikipedia.org/wiki/ROT13) before it is saved to the database.
131+
132+
![](images/eclipse_debug_User_password.png)
133+
134+
Try registering a new user using the debugger. In Eclipse, change the view or Perspective to the debugger by clicking on `Window` > `Perspective` > `Open Perspective` > `Debug`
135+
136+
![](images/eclipse_debug_perspective.png)
137+
138+
Eclipse will switch to the debug perspective. Since we enable remote debugging earlier, you should see the Daemon Threads for Tomcat in the debug window. Set a breakpoint for in the User class where the password is set.
139+
140+
![](images/eclipse_debug_User_breakpoint.png)
141+
142+
Register a new user with the username of 'Moby' and with 'm0by' as the password, click `Submit`, click `yes`
143+
144+
![](images/app_register_moby2.png)
145+
146+
Eclipse will display the code at the breakpoint and the value of password in the variables window. Note that the value is `m0by`
147+
148+
![](images/eclipse_debug_User_moby.png)
149+
150+
Click on `resume` or press `F8` to let the code run.
151+
152+
![](images/eclipse_debug_resume.png)
153+
154+
Next, set a breakpoint on the getPassword in the User class to see the value returned for password. You can also toggle off the breakpoint for setPassword.
155+
156+
![](images/eclipse_debug_User_getPassword.png)
157+
158+
Try to log into the application. Look at the value for password in the Eclipse variables window, note that it is `z0ol` which is `m0by` using ROT13.
159+
160+
![](images/eclipse_debug_User_show_user.png)
161+
162+
In this MVC application the UserController uses the findByLogin method in the UserServiceImpl class which uses the findByUsername method to retrieve the information from the database. It then checks to see if the password from the form matches the user password. Since the password from the login form is not scrambled using ROT13, it does not match the user password and you cannot log into the application.
163+
164+
To fix this, apply ROT13 to the password by adding
165+
166+
```
167+
import com.docker.UserSignup.utit.Rot13
168+
169+
String passwd = Rot13.rot13(password);
170+
```
171+
![](images/eclipse_debug_UserServiceImpl_code.png)
172+
173+
Set a breakpoint in UserServiceImpl on the findByLogin method. Log in again and look at the values for the breakpoint. The 'passwd' variable is `z0ol` which matches the password for the user moby.
174+
175+
![](images/eclipse_debug_UserServiceImpl_values.png)
176+
177+
Continue (`F8`) and you should successfully log in.
178+
179+
![](images/app_debug_success.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
## In-container Java Development: IntelliJ Community Edition
2+
3+
### Pre-requisites
4+
5+
* [Docker for OSX or Docker for Windows](https://www.docker.com/products/docker)
6+
* [IntelliJ Community Edition](https://www.jetbrains.com/idea/download/)
7+
* [Java Development Kit](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
8+
9+
10+
### Getting Started
11+
12+
In IntelliJ, clone the repository. Click on `Check out from Version Control` > `Github`
13+
14+
![](images/intelliJ_git_open_project.png)
15+
16+
If this the first time to use IntelliJ with Github, log into your Github account.
17+
![](images/intelliJ_git_login.png)
18+
19+
Clone the repository [registration-docker](https://github.com/spara/registration-docker.git).
20+
21+
![](images/intelliJ_git_clone_repository.png)
22+
Click on `Import project from external model`, select `Maven`. Click `Next`
23+
24+
![](images/intellij_github_import_maven.png)
25+
26+
Check `Search for projects recursively`. Click `Next`
27+
28+
![](images/intellij_github_import_maven_configure.png)
29+
30+
Select the project and click `Next`
31+
32+
![](images/intellij_github_import_maven_select.png)
33+
34+
Select the JDK and click `Next`
35+
36+
![](images/intellij_github_import_select_sdk.png)
37+
38+
Click `Finish`
39+
40+
![](images/intellij_github_import_project_finish.png)
41+
42+
Click on `Project View` to open the project.
43+
44+
![](images/intelliJ_git_open_project_gui.png)
45+
46+
### Building the application
47+
48+
The application is a basic Spring MVC application that receives user input from a form, writes the data to a database, and queries the database.
49+
50+
The application is built using Maven. To build the application click on icon on the bottom left of the IntelliJ window and select `Maven Projects`.
51+
52+
![](images/intellij_maven_setup.png)
53+
54+
The `Maven Projects` window will open ont the right side. Maven goals of `clean` and `install` need to be set to build the application.
55+
56+
To set the `clean` goal, click on `Lifecycle` to display the tree of goals. Right click on `clean` and select `Create 'UserSignup [clean]'...`
57+
58+
![](images/intellij_maven_goal_clean.png)
59+
60+
Click `OK` in the `Create Run/Debug Configuration` window.
61+
62+
![](images/intellij_maven_goal_clean_menu.png)
63+
64+
Configure the `install` goal similarly. Click on `install` in the Lifecycle tree. Select `Create 'UserSignup[install]'...`
65+
66+
![](images/intellij_maven_goal_install.png)
67+
68+
Click `OK` in the `Create Run/Debug Configuration` window.
69+
70+
![](images/intelligj_maven_goal_install_menu.png)
71+
72+
To build the application run `clean`
73+
74+
![](images/intellij_maven_goal_clean_run.png)
75+
76+
Then run `install`
77+
78+
![](images/intellij_maven_goal_install_run.png)
79+
80+
When the application builds you will see a success message in the log window.
81+
82+
![](images/intellij_maven_goal_install_log.png)
83+
84+
### Running the application
85+
86+
Open a terminal and go to the application directory. Start the application with docker-compose
87+
88+
<pre>&gt; docker-compose up </pre>
89+
90+
Docker will build the images for Apache Tomcat and MySQL and start the containers. It will also mount the application directory (`./app/target/UserSignup`) as a data volume on the host system to the Tomcat webapps directory in the web server container.
91+
92+
Open a browser window and go to:
93+
'localhost:8080'; you should see the Tomcat home page
94+
95+
![](images/tomcat_home3.png)
96+
97+
When the Tomcat image was built, the user roles were also configured. Click on the `Manager App` button to see the deployed applications. When prompted for username and password, enter `system` and `manager` respectively to log into the Tomcat Web Application Manager page.
98+
99+
![](images/tomcat_web_application_manager3.png)
100+
101+
You can use the Manager page to `Start`, `Stop`, `Reload` or `Undeploy` web applications.
102+
103+
To go to the application, Click on `/UserSignup` link.
104+
105+
![](images/app_index_page3.png)
106+
107+
### Debugging the Application
108+
109+
In the application, click on `Signup` to create a new user. Fill out the registration form and click `Submit`
110+
111+
![](images/app_debug_signup2.png)
112+
113+
Click `Yes` to confirm.
114+
115+
![](images/app_debug_signup_confirm.png)
116+
117+
Test out the login.
118+
119+
![](images/app_debug_login2.png)
120+
121+
Oh no!
122+
123+
![](images/app_debug_login_fail2.png)
124+
125+
#### Configure Remote Debugging
126+
127+
Tomcat supports remote debugging the Java Platform Debugger Architecture (JPDA). Remote debugging was enabled when the tomcat image (registration-webserver) was built.
128+
129+
To configure remote debugging in IntelliJ, click on `Run` > `Edit Configuration ...`
130+
131+
![](images/intelij_debug_run_edit_configurations.png)
132+
133+
Add a new remote configuration.
134+
135+
![](images/intellij_debug_add_remote_configuration.png)
136+
137+
In the `Run\Debug Configurations` window, set the `Name` of the configuration and in `Settings` set the port to '8000' the default Tomcat JPDA debuging port. Clcik on `OK` to save the configuration.
138+
139+
![](images/intellij_debug_tomcat_remote_settings.png)
140+
141+
#### Finding the Error
142+
143+
Since the problem is with the password, lets see how the password is set in the User class. In the User class, the setter for password is scrambled using [rot13](https://en.wikipedia.org/wiki/ROT13) before it is saved to the database.
144+
145+
![](images/intellij_debug_User_password.png)
146+
147+
Try registering a new user using the debugger. In the menu click on `Run` > `Debug...`
148+
149+
![](images/intellij_run_debug.png)
150+
151+
Choose the remote Tomcat debug configuration. The Debugger console will be displayed at the bottom of the IntelliJ window.
152+
153+
![](images/intellij_debug_choose_remote_tomcat.png)
154+
155+
Set a breakpoint for in the User class where the password is set.
156+
157+
![](images/intellij_debug_set_breakpoint_password.png)
158+
159+
Register a new user with the username of 'Moby' and with 'm0by' as the password, click `Submit`, click `yes`
160+
161+
![](images/app_register_moby2.png)
162+
163+
IntelliJ will display the code at the breakpoint and the value of password in the variables window. Note that the value is `m0by`
164+
165+
![](images/intellij_debug_User_moby.png)
166+
167+
Click on `Resume Program` to let the code run or press `F8` to step over the breakpoint.
168+
169+
![](images/intellij_debug_resume.png)
170+
171+
Next, set a breakpoint on the getPassword in the User class to see the value returned for password. You can also toggle off the breakpoint for setPassword.
172+
173+
![](images/intellij_debug_User_getPassword.png)
174+
175+
Try to log into the application. Look at the value for password in the debugging console, note that it is `z0ol` which is `m0by` using ROT13.
176+
177+
![](images/intellij_debug_User_show_user.png)
178+
179+
In this MVC application the UserController uses the findByLogin method in the UserServiceImpl class which uses the findByUsername method to retrieve the information from the database. It then checks to see if the password from the form matches the user password. Since the password from the login form is not scrambled using ROT13, it does not match the user password and you cannot log into the application.
180+
181+
To fix this, apply ROT13 to the password by adding
182+
183+
```
184+
import com.docker.UserSignup.utit.Rot13
185+
186+
String passwd = Rot13.rot13(password);
187+
```
188+
![](images/intellij_debug_UserServiceImpl_code.png)
189+
190+
Set a breakpoint in UserServiceImpl on the findByLogin method. Log in again and look at the values for the breakpoint. The 'passwd' variable is `z0ol` which matches the password for the user moby.
191+
192+
![](images/intellij_debug_UserServiceImpl_values.png)
193+
194+
Continue (`F8`) and you should successfully log in.
195+
196+
![](images/app_debug_success.png)

0 commit comments

Comments
 (0)