You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tb...@apache.org on 2015/04/27 20:19:20 UTC

ambari git commit: AMBARI-10767 - Views : create Spring app example (tbeerbower)

Repository: ambari
Updated Branches:
  refs/heads/trunk 1c46571f9 -> 6d737ddc6


AMBARI-10767 - Views : create Spring app example (tbeerbower)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/6d737ddc
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/6d737ddc
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/6d737ddc

Branch: refs/heads/trunk
Commit: 6d737ddc6daa9cae302fe79c4063cf1a0a975d6f
Parents: 1c46571
Author: tbeerbower <tb...@hortonworks.com>
Authored: Mon Apr 27 14:18:56 2015 -0400
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Mon Apr 27 14:19:08 2015 -0400

----------------------------------------------------------------------
 .../examples/hello-spring-view/docs/index.md    | 154 +++++++++++++++++++
 ambari-views/examples/hello-spring-view/pom.xml | 130 ++++++++++++++++
 .../ambari/view/hello/HelloController.java      |  50 ++++++
 .../src/main/resources/view.xml                 |  24 +++
 .../src/main/webapp/WEB-INF/Hello-servlet.xml   |  33 ++++
 .../src/main/webapp/WEB-INF/jsp/hello.jsp       |  26 ++++
 .../src/main/webapp/WEB-INF/web.xml             |  37 +++++
 ambari-views/examples/pom.xml                   |   1 +
 8 files changed, 455 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/docs/index.md
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/docs/index.md b/ambari-views/examples/hello-spring-view/docs/index.md
new file mode 100644
index 0000000..b621db2
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/docs/index.md
@@ -0,0 +1,154 @@
+<!---
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+Hello Spring View Example
+========
+Description
+-----
+The Hello Spring view is a very simple view example that uses Spring MVC.  Like the HelloWorld view example, it demonstrates the basics of how to write and deploy a view in Ambari but also shows a simple web application using the Spring MVC framework.  The Hello Spring view displays a Hello message that is customized based on the currently logged in user.
+
+Package
+-----
+
+All views are packaged as a view archive.  The view archive contains the configuration file and various optional components of the view.
+
+#####view.xml
+
+The view.xml file is the only required file for a view archive.  The view.xml is the configuration that describes the view and view instances for Ambari.
+
+      <view>
+        <name>HELLO_SPRING</name>
+        <label>The Hello Spring View</label>
+        <version>1.0.0</version>
+        <instance>
+          <name>INSTANCE</name>
+        </instance>
+      </view>
+
+The configuration in this case defines a view named HELLO_SPRING that has a single instance.
+
+
+#####HelloController.java
+
+The HelloController class is the controller for the Spring MVC app.
+
+Notice that we can access the view context through the servlet context.
+
+    // get the view context from the servlet context
+    ViewContext viewContext = (ViewContext) request.getSession().getServletContext().getAttribute(ViewContext.CONTEXT_ATTRIBUTE);
+
+    // get the current user name from the view context
+    String userName = viewContext.getUsername();
+
+
+For this app, the controller saves a customized greeting to a model attribute.
+
+    // add the greeting message attribute
+    model.addAttribute("greeting", "Hello " + (userName == null ? "unknown user" : userName) + "!");
+
+
+
+#####WEB-INF/web.xml
+The web.xml is the deployment descriptor used to deploy the view as a web app.  The Java EE standards apply for the descriptor.  We can see that for this example a single servlet is mapped to the root context path.  The class for the servlet is the Spring DispatcherServlet.
+
+        <servlet>
+          <servlet-name>Hello</servlet-name>
+          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+          <load-on-startup>0</load-on-startup>
+        </servlet>
+        <servlet-mapping>
+          <servlet-name>Hello</servlet-name>
+          <url-pattern>/</url-pattern>
+        </servlet-mapping>
+      </web-app>
+
+#####WEB-INF/hello-spring.xml
+
+This hello-spring.xml file contains the bean configuration.
+
+      <context:component-scan base-package="org.apache.ambari.view.hello" />
+
+      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+        <property name="prefix" value="/WEB-INF/jsp/" />
+        <property name="suffix" value=".jsp" />
+      </bean>
+
+In the above servlet.xml file, we have defined a tag <context:component-scan> . This will direct Spring to load all the components from the org.apache.ambari.view.hello package.
+
+Note that in the HelloController class we return the bean name "hello" which will resolve to the view /WEB-INF/jsp/hello.jsp.
+
+
+#####WEB-INF/jsp/hello.jsp
+
+The Spring MVC app view for this example will display the customized greeting message saved in the HelloController class.
+
+      <%@ page contentType="text/html; charset=UTF-8" %>
+      <html>
+        <head>
+          <title>Hello</title>
+        </head>
+        <body>
+          <h2>${greeting}</h2>
+        </body>
+      </html>
+
+Build
+-----
+
+The view can be built as a maven project.
+
+    cd ambari-views/examples/hello-spring-view
+    mvn clean package
+
+The build will produce the view archive.
+
+    ambari-views/examples/hello-spring-view/target/hello-spring-view-x.x.x.war
+
+
+Deploy
+-----
+To deploy a view we simply place the view archive in the views folder of the ambari-server machine.  By default the views folder is located at ...
+
+    /var/lib/ambari-server/resources/views
+
+To deploy the Hello Spring view simply copy the hello-spring-view jar to the ambari-server views folder and restart the ambari server.
+
+Use
+-----
+
+After deploying a view you should see it as a view resource in the Ambari REST API.  If we request all views, we should see the HELLO_SPRING view.
+
+      http://<server>:8080/api/v1/views
+
+      {
+        "href" : "http://<server>:8080/api/v1/views",
+        "items" : [
+          {
+            "href" : "http://c6401.ambari.apache.org:8080/api/v1/views/HELLO_SPRING",
+            "ViewInfo" : {
+              "view_name" : "HELLO_SPRING"
+            }
+          },...
+        ]
+      }
+
+
+We can access access the view at ...
+      
+    http://c6401.ambari.apache.org:8080/views/HELLO_SPRING/1.0.0/INSTANCE/
+
+This should display a greeting customized to for the logged in user.
+
+

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/pom.xml b/ambari-views/examples/hello-spring-view/pom.xml
new file mode 100644
index 0000000..833e25c
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/pom.xml
@@ -0,0 +1,130 @@
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<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/maven-v4_0_0.xsd">
+  <parent>
+    <groupId>org.apache.ambari</groupId>
+    <artifactId>ambari-view-examples</artifactId>
+    <version>2.0.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>hello-spring-view</artifactId>
+  <packaging>war</packaging>
+  <name>Ambari Hello Spring View</name>
+  <version>2.0.0-SNAPSHOT</version>
+  <url>http://maven.apache.org</url>
+  <properties>
+    <ambari.dir>${project.parent.parent.parent.basedir}</ambari.dir>
+  </properties>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <version>3.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.ambari</groupId>
+      <artifactId>ambari-views</artifactId>
+      <version>[1.7.0.0,)</version>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-server</artifactId>
+      <version>1.8</version>
+    </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.5</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-context</artifactId>
+      <version>4.0.5.RELEASE</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-core</artifactId>
+      <version>4.0.5.RELEASE</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-web</artifactId>
+      <version>4.0.5.RELEASE</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-webmvc</artifactId>
+      <version>4.0.5.RELEASE</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-beans</artifactId>
+      <version>4.0.5.RELEASE</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-expression</artifactId>
+      <version>4.0.5.RELEASE</version>
+    </dependency>
+
+  </dependencies>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.12</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>rpm-maven-plugin</artifactId>
+        <version>2.0.1</version>
+        <executions>
+          <execution>
+            <phase>none</phase>
+            <goals>
+              <goal>rpm</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-war-plugin</artifactId>
+        <configuration>
+          <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
+          <packagingExcludes>WEB-INF/lib/ambari-views*.jar</packagingExcludes>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/src/main/java/org/apache/ambari/view/hello/HelloController.java
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/src/main/java/org/apache/ambari/view/hello/HelloController.java b/ambari-views/examples/hello-spring-view/src/main/java/org/apache/ambari/view/hello/HelloController.java
new file mode 100644
index 0000000..665e7af
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/src/main/java/org/apache/ambari/view/hello/HelloController.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hello;
+
+import org.apache.ambari.view.ViewContext;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Controller for the Hello Spring app view.
+ */
+@Controller
+@RequestMapping(value = "/")
+public class HelloController {
+
+  @RequestMapping(method = RequestMethod.GET)
+  public String printHello(ModelMap model, HttpServletRequest request) {
+
+    // get the view context from the servlet context
+    ViewContext viewContext = (ViewContext) request.getSession().getServletContext().getAttribute(ViewContext.CONTEXT_ATTRIBUTE);
+
+    // get the current user name from the view context
+    String userName = viewContext.getUsername();
+
+    // add the greeting message attribute
+    model.addAttribute("greeting", "Hello " + (userName == null ? "unknown user" : userName) + "!");
+
+    return "hello";
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/src/main/resources/view.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/src/main/resources/view.xml b/ambari-views/examples/hello-spring-view/src/main/resources/view.xml
new file mode 100644
index 0000000..fcd84a6
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/src/main/resources/view.xml
@@ -0,0 +1,24 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
+-->
+<view>
+  <name>HELLO_SPRING</name>
+  <label>The Hello Spring View</label>
+  <version>1.0.0</version>
+  <instance>
+    <name>INSTANCE</name>
+  </instance>
+</view>

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/Hello-servlet.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/Hello-servlet.xml b/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/Hello-servlet.xml
new file mode 100644
index 0000000..6864b84
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/Hello-servlet.xml
@@ -0,0 +1,33 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+   http://www.springframework.org/schema/beans
+   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+   http://www.springframework.org/schema/context
+   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+  <context:component-scan base-package="org.apache.ambari.view.hello" />
+
+  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+    <property name="prefix" value="/WEB-INF/jsp/" />
+    <property name="suffix" value=".jsp" />
+  </bean>
+</beans>

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/jsp/hello.jsp
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/jsp/hello.jsp b/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/jsp/hello.jsp
new file mode 100644
index 0000000..ec7b424
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/jsp/hello.jsp
@@ -0,0 +1,26 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
+-->
+
+<%@ page contentType="text/html; charset=UTF-8" %>
+<html>
+  <head>
+    <title>Hello</title>
+  </head>
+  <body>
+    <h2>${greeting}</h2>
+  </body>
+</html>

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/web.xml b/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..c212207
--- /dev/null
+++ b/ambari-views/examples/hello-spring-view/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
+-->
+
+<web-app id="WebApp_ID" version="2.4"
+         xmlns="http://java.sun.com/xml/ns/j2ee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
+   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+
+  <display-name>Spring MVC Application</display-name>
+
+  <servlet>
+    <servlet-name>Hello</servlet-name>
+    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+    <load-on-startup>0</load-on-startup>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>Hello</servlet-name>
+    <url-pattern>/</url-pattern>
+  </servlet-mapping>
+</web-app>

http://git-wip-us.apache.org/repos/asf/ambari/blob/6d737ddc/ambari-views/examples/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/pom.xml b/ambari-views/examples/pom.xml
index adb76b1..de55ded 100644
--- a/ambari-views/examples/pom.xml
+++ b/ambari-views/examples/pom.xml
@@ -31,6 +31,7 @@
   <modules>
     <module>helloworld-view</module>
     <module>hello-servlet-view</module>
+    <module>hello-spring-view</module>
     <module>favorite-view</module>
     <module>phone-list-view</module>
     <module>calculator-view</module>