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/06/01 15:19:48 UTC
ambari git commit: AMBARI-11583 - Views : Persistence Data Store
Example (tbeerbower)
Repository: ambari
Updated Branches:
refs/heads/trunk 6f13d10ac -> 3258b649e
AMBARI-11583 - Views : Persistence Data Store Example (tbeerbower)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/3258b649
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/3258b649
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/3258b649
Branch: refs/heads/trunk
Commit: 3258b649e2bf0131e7d0c131a816b78f4dbfc091
Parents: 6f13d10
Author: tbeerbower <tb...@hortonworks.com>
Authored: Mon Jun 1 09:19:28 2015 -0400
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Mon Jun 1 09:19:38 2015 -0400
----------------------------------------------------------------------
.../ambari/view/phonelist/PhoneListServlet.java | 151 +++++++++++++++----
.../apache/ambari/view/phonelist/PhoneUser.java | 88 +++++++++++
.../phone-list-view/src/main/resources/view.xml | 23 +++
3 files changed, 233 insertions(+), 29 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/3258b649/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneListServlet.java
----------------------------------------------------------------------
diff --git a/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneListServlet.java b/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneListServlet.java
index d0fbbef..146a8e3 100644
--- a/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneListServlet.java
+++ b/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneListServlet.java
@@ -18,7 +18,9 @@
package org.apache.ambari.view.phonelist;
+import org.apache.ambari.view.DataStore;
import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.PersistenceException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
@@ -28,6 +30,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -41,54 +45,78 @@ public class PhoneListServlet extends HttpServlet {
*/
private ViewContext viewContext;
+ /**
+ * The view data store.
+ * <code>null</code> indicates that the view properties should be used instead of the data store.
+ */
+ private DataStore dataStore = null;
+
+
+ // ----- GenericServlet ----------------------------------------------------
+
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = config.getServletContext();
viewContext = (ViewContext) context.getAttribute(ViewContext.CONTEXT_ATTRIBUTE);
+ dataStore = Boolean.parseBoolean(viewContext.getProperties().get("data.store.enabled")) ?
+ viewContext.getDataStore() : null;
}
+
+ // ----- HttpServlet -------------------------------------------------------
+
@Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
String name = request.getParameter("name");
String phone = request.getParameter("phone");
- if (name != null && name.length() > 0 && phone != null && phone.length() > 0) {
- if (request.getParameter("add") != null) {
- if (viewContext.getInstanceData(name) != null) {
- throw new IllegalArgumentException("A number for " + name + " already exists.");
+ try {
+ if (name != null && name.length() > 0 && phone != null && phone.length() > 0) {
+ if (request.getParameter("add") != null) {
+ addUser(name, phone);
+ } else if (request.getParameter("update") != null) {
+ updateUser(name, phone);
+ } else if (request.getParameter("delete") != null) {
+ removeUser(name, phone);
}
- viewContext.putInstanceData(name, phone);
- } else if (request.getParameter("update") != null) {
- viewContext.putInstanceData(name, phone);
- } else if (request.getParameter("delete") != null) {
- viewContext.removeInstanceData(name);
}
+ listAll(request, response);
+ } catch (Exception e) {
+ throw new ServletException(e);
}
- listAll(request, response);
}
@Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter writer = response.getWriter();
+ try {
+ String name = request.getParameter("name");
+ String phone = getPhone(name);
- String name = request.getParameter("name");
- String phone = (name == null || name.length() == 0) ? null : viewContext.getInstanceData(name);
-
- if (phone != null) {
- editNumber(writer, name, phone);
- } else {
- listAll(request, response);
+ if (phone != null) {
+ editNumber(writer, name, phone, request);
+ } else {
+ listAll(request, response);
+ }
+ } catch (PersistenceException e) {
+ throw new ServletException(e);
}
}
- private void enterNumber(PrintWriter writer) {
- writer.println("<form name=\"input\" method=\"POST\">");
+
+ // ----- helper methods ----------------------------------------------------
+
+ // form to add new user
+ private void enterNumber(PrintWriter writer, HttpServletRequest request) {
+ writer.println("<form name=\"input\" action = \""+ request.getRequestURI() +"\" method=\"POST\">");
writer.println("<table>");
writer.println("<tr>");
writer.println("<td>Name:</td><td><input type=\"text\" name=\"name\"></td><br/>");
@@ -101,8 +129,9 @@ public class PhoneListServlet extends HttpServlet {
writer.println("</form>");
}
- private void editNumber(PrintWriter writer, String name, String phone) {
- writer.println("<form name=\"input\" method=\"POST\">");
+ // for to update / delete existing user
+ private void editNumber(PrintWriter writer, String name, String phone, HttpServletRequest request) {
+ writer.println("<form name=\"input\" action = \""+ request.getRequestURI() +"\" method=\"POST\">");
writer.println("<table>");
writer.println("<tr>");
writer.println("<td>Name:</td><td><input type=\"text\" name=\"name\" value=\"" + name + "\" readonly></td><br/>");
@@ -116,8 +145,8 @@ public class PhoneListServlet extends HttpServlet {
writer.println("</form>");
}
- private void listAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
- String name;Map<String, String> data = new LinkedHashMap<String, String>(viewContext.getInstanceData());
+ // list all of the users
+ private void listAll(HttpServletRequest request, HttpServletResponse response) throws IOException, PersistenceException {
PrintWriter writer = response.getWriter();
@@ -129,16 +158,80 @@ public class PhoneListServlet extends HttpServlet {
writer.println("<td>Phone Number</td>");
writer.println("</tr>");
- for (Map.Entry<String,String> entry : data.entrySet()){
- name = entry.getKey();
+ Collection<PhoneUser> phoneUsers = getAllUsers();
+ for (PhoneUser phoneUser : phoneUsers) {
+ String name = phoneUser.getName();
writer.println("<tr>");
writer.println("<td><A href=" + request.getRequestURI() + "?name=" + name + ">" + name + "</A></td>");
- writer.println("<td>" + entry.getValue() + "</td>");
+ writer.println("<td>" + phoneUser.getPhone() + "</td>");
writer.println("</tr>");
}
+
writer.println("</table><br/><hr/>");
- enterNumber(writer);
+ enterNumber(writer, request);
+ }
+
+ // determine whether a user has been persisted
+ private boolean userExists(String name) throws PersistenceException {
+ return dataStore != null &&
+ dataStore.find(PhoneUser.class, name) != null || viewContext.getInstanceData(name) != null;
+ }
+
+ // persist a new user
+ private void addUser(String name, String phone) throws PersistenceException {
+ if (userExists(name)) {
+ throw new IllegalArgumentException("A number for " + name + " already exists.");
+ }
+ updateUser(name, phone);
+ }
+
+ // update an existing user
+ private void updateUser(String name, String phone) throws PersistenceException {
+ if (dataStore != null) {
+ dataStore.store(new PhoneUser(name, phone));
+ } else {
+ viewContext.putInstanceData(name, phone);
+ }
+ }
+
+ // remove an existing user
+ private void removeUser(String name, String phone) throws PersistenceException {
+ if (dataStore != null) {
+ dataStore.remove(new PhoneUser(name, phone));
+ } else {
+ viewContext.removeInstanceData(name);
+ }
+ }
+
+ // get the phone for the given name
+ private String getPhone(String name) throws PersistenceException {
+ if (name != null && name.length() > 0) {
+ if (dataStore != null) {
+ PhoneUser phoneUser = dataStore.find(PhoneUser.class, name);
+ if (phoneUser != null) {
+ return phoneUser.getPhone();
+ }
+ } else {
+ return viewContext.getInstanceData(name);
+ }
+ }
+ return null;
+ }
+
+ // get all of the phone users
+ private Collection<PhoneUser> getAllUsers() throws PersistenceException {
+ if (dataStore != null) {
+ return dataStore.findAll(PhoneUser.class, null);
+ }
+ Map<String, String> data = new LinkedHashMap<String, String>(viewContext.getInstanceData());
+ Collection<PhoneUser> users = new HashSet<PhoneUser>();
+
+ for (Map.Entry<String,String> entry : data.entrySet()) {
+ users.add(new PhoneUser(entry.getKey(), entry.getValue()));
+ entry.getKey();
+ }
+ return users;
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/3258b649/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneUser.java
----------------------------------------------------------------------
diff --git a/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneUser.java b/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneUser.java
new file mode 100644
index 0000000..6b3ce28
--- /dev/null
+++ b/ambari-views/examples/phone-list-view/src/main/java/org/apache/ambari/view/phonelist/PhoneUser.java
@@ -0,0 +1,88 @@
+/*
+ * 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.phonelist;
+
+/**
+ * Phone user (name/phone number) entity for the phone list view example.
+ */
+public class PhoneUser {
+
+ /**
+ * The user name.
+ */
+ private String name;
+
+ /**
+ * The phone number.
+ */
+ private String phone;
+
+ /**
+ * No-arg constructor required for JPA.
+ */
+ public PhoneUser() {
+ }
+
+ /**
+ * Construct a phone user.
+ *
+ * @param name the user name
+ * @param phone the phone number
+ */
+ public PhoneUser(String name, String phone) {
+ this.name = name;
+ this.phone = phone;
+ }
+
+ /**
+ * Get the user name.
+ *
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Set the user name.
+ *
+ * @param name the name
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * Get the phone number.
+ *
+ * @return the phone number
+ */
+ public String getPhone() {
+ return phone;
+ }
+
+ /**
+ * Set the phone number.
+ *
+ * @param phone the phone number
+ */
+ public void setPhone(String phone) {
+ this.phone = phone;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/3258b649/ambari-views/examples/phone-list-view/src/main/resources/view.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/phone-list-view/src/main/resources/view.xml b/ambari-views/examples/phone-list-view/src/main/resources/view.xml
index 399b3c9..bd927ca 100644
--- a/ambari-views/examples/phone-list-view/src/main/resources/view.xml
+++ b/ambari-views/examples/phone-list-view/src/main/resources/view.xml
@@ -18,10 +18,33 @@ limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
<name>PHONE_LIST</name>
<label>The Phone List View</label>
<version>1.0.0</version>
+ <parameter>
+ <name>data.store.enabled</name>
+ <description>
+ Determine whether or not to use the view persistence data store.
+ A value of false indicates that the view properties should be used instead of the data store.
+ </description>
+ <default-value>false</default-value>
+ <required>false</required>
+ </parameter>
+ <persistence>
+ <entity>
+ <class>org.apache.ambari.view.phonelist.PhoneUser</class>
+ <id-property>name</id-property>
+ </entity>
+ </persistence>
<instance>
<name>LIST_1</name>
+ <property>
+ <key>data.store.enabled</key>
+ <value>false</value>
+ </property>
</instance>
<instance>
<name>LIST_2</name>
+ <property>
+ <key>data.store.enabled</key>
+ <value>true</value>
+ </property>
</instance>
</view>
\ No newline at end of file