Skip to content

Commit 9663b75

Browse files
committed
Updated sample
1 parent 268a5b7 commit 9663b75

File tree

7 files changed

+1766
-1
lines changed

7 files changed

+1766
-1
lines changed

.gitignore

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
*.class
2+
3+
# Auth filed
4+
*.auth
5+
*.azureauth
6+
7+
# Mobile Tools for Java (J2ME)
8+
.mtj.tmp/
9+
10+
# Package Files #
11+
*.jar
12+
*.war
13+
*.ear
14+
15+
# Azure Tooling #
16+
node_modules
17+
packages
18+
19+
# Eclipse #
20+
*.pydevproject
21+
.project
22+
.metadata
23+
bin/**
24+
tmp/**
25+
tmp/**/*
26+
*.tmp
27+
*.bak
28+
*.swp
29+
*~.nib
30+
local.properties
31+
.classpath
32+
.settings/
33+
.loadpath
34+
35+
# Other Tooling #
36+
.classpath
37+
.project
38+
target
39+
.idea
40+
*.iml
41+
42+
# Mac OS #
43+
.DS_Store
44+
.DS_Store?
45+
46+
# Windows #
47+
Thumbs.db
48+
49+
# reduced pom files should not be included
50+
dependency-reduced-pom.xml

CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributing to Azure samples
2+
3+
Thank you for your interest in contributing to Azure samples!
4+
5+
## Ways to contribute
6+
7+
You can contribute to [Azure samples](https://azure.microsoft.com/documentation/samples/) in a few different ways:
8+
9+
- Submit feedback on [this sample page](https://azure.microsoft.com/documentation/samples/app-service-java-manage-web-apps/) whether it was helpful or not.
10+
- Submit issues through [issue tracker](https://github.com/Azure-Samples/app-service-java-manage-web-apps/issues) on GitHub. We are actively monitoring the issues and improving our samples.
11+
- If you wish to make code changes to samples, or contribute something new, please follow the [GitHub Forks / Pull requests model](https://help.github.com/articles/fork-a-repo/): Fork the sample repo, make the change and propose it back by submitting a pull request.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Microsoft Corporation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
# app-service-java-manage-web-apps
1+
---
2+
services: Appservice
3+
platforms: java
4+
author: jianghaolu
5+
---
6+
7+
#Getting Started with Appservice - Manage Web App Basic - in Java #
8+
9+
10+
Azure App Service basic sample for managing web apps.
11+
- Create 3 web apps under the same new app service plan:
12+
- 1, 2 are in the same resource group, 3 in a different one
13+
- Stop and start 1, restart 2
14+
- Add Java support to app 3
15+
- List web apps
16+
- Delete a web app
17+
18+
19+
## Running this Sample ##
20+
21+
To run this sample:
22+
23+
Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-sdk-for-java/blob/master/AUTH.md).
24+
25+
git clone https://github.com/Azure-Samples/app-service-java-manage-web-apps.git
26+
27+
cd app-service-java-manage-web-apps
28+
29+
mvn clean compile exec:java
30+
31+
## More information ##
32+
33+
[http://azure.com/java] (http://azure.com/java)
34+
35+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
36+
37+
---
38+
39+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

pom.xml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.microsoft.azure</groupId>
4+
<artifactId>app-service-java-manage-web-apps</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>ManageWebAppBasic</name>
7+
<description></description>
8+
<url>https://github.com/Azure/app-service-java-manage-web-apps</url>
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.microsoft.azure</groupId>
12+
<artifactId>azure</artifactId>
13+
<version>1.0.0-beta4</version>
14+
</dependency>
15+
</dependencies>
16+
<build>
17+
<sourceDirectory>src</sourceDirectory>
18+
<resources>
19+
<resource>
20+
<directory>resources</directory>
21+
</resource>
22+
</resources>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.codehaus.mojo</groupId>
26+
<artifactId>exec-maven-plugin</artifactId>
27+
<version>1.4.0</version>
28+
<configuration>
29+
<mainClass>com.microsoft.azure.management.appservice.samples.ManageWebAppBasic</mainClass>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>3.0</version>
36+
<configuration>
37+
<source>1.7</source>
38+
<target>1.7</target>
39+
</configuration>
40+
</plugin>
41+
<plugin>
42+
<!-- Generate a fully packaged executable jar with dependencies -->
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-assembly-plugin</artifactId>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>attached</goal>
49+
</goals>
50+
<phase>package</phase>
51+
<configuration>
52+
<descriptorRefs>
53+
<descriptorRef>jar-with-dependencies</descriptorRef>
54+
</descriptorRefs>
55+
<archive>
56+
<manifest>
57+
<mainClass>com.microsoft.azure.management.appservice.samples.ManageWebAppBasic</mainClass>
58+
</manifest>
59+
</archive>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/**
2+
*
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*
6+
*/
7+
8+
package com.microsoft.azure.management.appservice.samples;
9+
10+
import com.microsoft.azure.management.Azure;
11+
import com.microsoft.azure.management.appservice.AppServicePlan;
12+
import com.microsoft.azure.management.appservice.AppServicePricingTier;
13+
import com.microsoft.azure.management.appservice.JavaVersion;
14+
import com.microsoft.azure.management.appservice.WebApp;
15+
import com.microsoft.azure.management.appservice.WebContainer;
16+
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
17+
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
18+
import com.microsoft.azure.management.samples.Utils;
19+
import okhttp3.logging.HttpLoggingInterceptor;
20+
21+
import java.io.File;
22+
23+
/**
24+
* Azure App Service basic sample for managing web apps.
25+
* - Create 3 web apps under the same new app service plan:
26+
* - 1, 2 are in the same resource group, 3 in a different one
27+
* - Stop and start 1, restart 2
28+
* - Add Java support to app 3
29+
* - List web apps
30+
* - Delete a web app
31+
*/
32+
public final class ManageWebAppBasic {
33+
34+
/**
35+
* Main entry point.
36+
* @param args the parameters
37+
*/
38+
public static void main(String[] args) {
39+
// New resources
40+
final String app1Name = ResourceNamer.randomResourceName("webapp1-", 20);
41+
final String app2Name = ResourceNamer.randomResourceName("webapp2-", 20);
42+
final String app3Name = ResourceNamer.randomResourceName("webapp3-", 20);
43+
final String planName = ResourceNamer.randomResourceName("jplan_", 15);
44+
final String rg1Name = ResourceNamer.randomResourceName("rg1NEMV_", 24);
45+
final String rg2Name = ResourceNamer.randomResourceName("rg2NEMV_", 24);
46+
47+
try {
48+
49+
//=============================================================
50+
// Authenticate
51+
52+
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
53+
54+
Azure azure = Azure
55+
.configure()
56+
.withLogLevel(HttpLoggingInterceptor.Level.NONE)
57+
.authenticate(credFile)
58+
.withDefaultSubscription();
59+
60+
// Print selected subscription
61+
System.out.println("Selected subscription: " + azure.subscriptionId());
62+
try {
63+
64+
65+
//============================================================
66+
// Create a web app with a new app service plan
67+
68+
System.out.println("Creating web app " + app1Name + " in resource group " + rg1Name + "...");
69+
70+
WebApp app1 = azure.webApps()
71+
.define(app1Name)
72+
.withNewResourceGroup(rg1Name)
73+
.withNewAppServicePlan(planName)
74+
.withRegion(Region.US_WEST)
75+
.withPricingTier(AppServicePricingTier.STANDARD_S1)
76+
.create();
77+
78+
System.out.println("Created web app " + app1.name());
79+
Utils.print(app1);
80+
81+
//============================================================
82+
// Create a second web app with the same app service plan
83+
84+
System.out.println("Creating another web app " + app2Name + " in resource group " + rg1Name + "...");
85+
AppServicePlan plan = azure.appServices().appServicePlans().getByGroup(rg1Name, planName);
86+
WebApp app2 = azure.webApps()
87+
.define(app2Name)
88+
.withExistingResourceGroup(rg1Name)
89+
.withExistingAppServicePlan(plan)
90+
.create();
91+
92+
System.out.println("Created web app " + app2.name());
93+
Utils.print(app2);
94+
95+
//============================================================
96+
// Create a third web app with the same app service plan, but
97+
// in a different resource group
98+
99+
System.out.println("Creating another web app " + app3Name + " in resource group " + rg2Name + "...");
100+
WebApp app3 = azure.webApps()
101+
.define(app3Name)
102+
.withNewResourceGroup(rg2Name)
103+
.withExistingAppServicePlan(plan)
104+
.create();
105+
106+
System.out.println("Created web app " + app3.name());
107+
Utils.print(app3);
108+
109+
//============================================================
110+
// stop and start app1, restart app 2
111+
System.out.println("Stopping web app " + app1.name());
112+
app1.stop();
113+
System.out.println("Stopped web app " + app1.name());
114+
Utils.print(app1);
115+
System.out.println("Starting web app " + app1.name());
116+
app1.start();
117+
System.out.println("Started web app " + app1.name());
118+
Utils.print(app1);
119+
System.out.println("Restarting web app " + app2.name());
120+
app2.restart();
121+
System.out.println("Restarted web app " + app2.name());
122+
Utils.print(app2);
123+
124+
//============================================================
125+
// Configure app 3 to have Java 8 enabled
126+
System.out.println("Adding Java support to web app " + app3Name + "...");
127+
app3.update()
128+
.withJavaVersion(JavaVersion.JAVA_8_NEWEST)
129+
.withWebContainer(WebContainer.TOMCAT_8_0_NEWEST)
130+
.apply();
131+
System.out.println("Java supported on web app " + app3Name + "...");
132+
133+
//=============================================================
134+
// List web apps
135+
136+
System.out.println("Printing list of web apps in resource group " + rg1Name + "...");
137+
138+
for (WebApp webApp : azure.webApps().listByGroup(rg1Name)) {
139+
Utils.print(webApp);
140+
}
141+
142+
System.out.println("Printing list of web apps in resource group " + rg2Name + "...");
143+
144+
for (WebApp webApp : azure.webApps().listByGroup(rg2Name)) {
145+
Utils.print(webApp);
146+
}
147+
148+
//=============================================================
149+
// Delete a web app
150+
151+
System.out.println("Deleting web app " + app1Name + "...");
152+
azure.webApps().deleteByGroup(rg1Name, app1Name);
153+
System.out.println("Deleted web app " + app1Name + "...");
154+
155+
System.out.println("Printing list of web apps in resource group " + rg1Name + " again...");
156+
for (WebApp webApp : azure.webApps().listByGroup(rg1Name)) {
157+
Utils.print(webApp);
158+
}
159+
160+
} catch (Exception e) {
161+
System.err.println(e.getMessage());
162+
e.printStackTrace();
163+
} finally {
164+
try {
165+
System.out.println("Deleting Resource Group: " + rg1Name);
166+
azure.resourceGroups().beginDeleteByName(rg1Name);
167+
System.out.println("Deleted Resource Group: " + rg1Name);
168+
System.out.println("Deleting Resource Group: " + rg2Name);
169+
azure.resourceGroups().beginDeleteByName(rg2Name);
170+
System.out.println("Deleted Resource Group: " + rg2Name);
171+
} catch (NullPointerException npe) {
172+
System.out.println("Did not create any resources in Azure. No clean up is necessary");
173+
} catch (Exception g) {
174+
g.printStackTrace();
175+
}
176+
}
177+
178+
} catch (Exception e) {
179+
System.out.println(e.getMessage());
180+
e.printStackTrace();
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)