ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject [2/3] AMBARI-5257 - Ambari Views : Persistence
Date Sun, 30 Mar 2014 23:59:37 GMT
http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntity.java
new file mode 100644
index 0000000..9c8a3b2
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntity.java
@@ -0,0 +1,266 @@
+/**
+ * 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.server.orm.entities;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Represents a resource of a View.
+ */
+@javax.persistence.IdClass(ViewResourceEntityPK.class)
+@Table(name = "viewresource")
+@Entity
+public class ViewResourceEntity {
+  /**
+   * The view name.
+   */
+  @Id
+  @Column(name = "view_name", nullable = false, insertable = false, updatable = false)
+  private String viewName;
+
+  /**
+   * The resource name.
+   */
+  @Id
+  @Column(name = "name", nullable = false, insertable = true, updatable = false)
+  private String name;
+
+  /**
+   * The plural name of the resource.
+   */
+  @Column(name = "plural_name")
+  @Basic
+  private String pluralName;
+
+  /**
+   * The id property of the resource.
+   */
+  @Column(name = "id_property")
+  @Basic
+  private String idProperty;
+
+  /**
+   * The list of sub resource names.
+   */
+  @Column(name = "subResource_names")
+  @Basic
+  private String subResourceNames;
+
+  /**
+   * The resource provider class name.
+   */
+  @Column
+  @Basic
+  private String provider;
+
+  /**
+   * The resource service class name.
+   */
+  @Column
+  @Basic
+  private String service;
+
+  /**
+   * The resource class name.
+   */
+  @Column
+  @Basic
+  private String resource;
+
+  /**
+   * The view entity.
+   */
+  @ManyToOne
+  @JoinColumn(name = "view_name", referencedColumnName = "view_name", nullable = false)
+  private ViewEntity view;
+
+
+  // ----- ViewResourceEntity ------------------------------------------------
+
+  /**
+   * Get the view name.
+   *
+   * @return the view name
+   */
+  public String getViewName() {
+    return viewName;
+  }
+
+  /**
+   * Set the view name
+   *
+   * @param viewName  the view name
+   */
+  public void setViewName(String viewName) {
+    this.viewName = viewName;
+  }
+
+  /**
+   * Get the resource name.
+   *
+   * @return the resource name
+   */
+  public String getName() {
+    return name;
+  }
+
+  /**
+   * Set the resource name.
+   *
+   * @param name  the resource name
+   */
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  /**
+   * Get the resource plural name.
+   *
+   * @return the resource plural name
+   */
+  public String getPluralName() {
+    return pluralName;
+  }
+
+  /**
+   * Set the resource plural name.
+   *
+   * @param pluralName  the plural name
+   */
+  public void setPluralName(String pluralName) {
+    this.pluralName = pluralName;
+  }
+
+  /**
+   * Get the id property.
+   *
+   * @return the id property
+   */
+  public String getIdProperty() {
+    return idProperty;
+  }
+
+  /**
+   * Set the id property.
+   *
+   * @param idProperty  the id property
+   */
+  public void setIdProperty(String idProperty) {
+    this.idProperty = idProperty;
+  }
+
+  /**
+   * Get the sub-resource names.
+   *
+   * @return the sub resource names
+   */
+  public Collection<String> getSubResourceNames() {
+    return Arrays.asList(subResourceNames.split("\\s*,\\s*"));
+  }
+
+  /**
+   * Set the sub-resource names.
+   *
+   * @param subResourceNames  the sub-resource names
+   */
+  public void setSubResourceNames(Collection<String> subResourceNames) {
+    String s = subResourceNames.toString();
+    this.subResourceNames = subResourceNames.size() > 0 ? s.substring(1, s.length()-1) : null;
+  }
+
+  /**
+   * Get the resource provider class name.
+   *
+   * @return the resource provider class name.
+   */
+  public String getProvider() {
+    return provider;
+  }
+
+  /**
+   * Set the resource provider class name.
+   *
+   * @param provider  the resource provider class name.
+   */
+  public void setProvider(String provider) {
+    this.provider = provider;
+  }
+
+  /**
+   * Get the resource service class name.
+   *
+   * @return the resource service class name
+   */
+  public String getService() {
+    return service;
+  }
+
+  /**
+   * Set the resource service class name.
+   *
+   * @param service  the resource service class name
+   */
+  public void setService(String service) {
+    this.service = service;
+  }
+
+  /**
+   * Get the resource class name.
+   *
+   * @return the resource class name
+   */
+  public String getResource() {
+    return resource;
+  }
+
+  /**
+   * Set the resource class name.
+   *
+   * @param resource  the resource class name
+   */
+  public void setResource(String resource) {
+    this.resource = resource;
+  }
+
+  /**
+   * Get the parent view entity.
+   *
+   * @return the view entity
+   */
+  public ViewEntity getViewEntity() {
+    return view;
+  }
+
+  /**
+   * Set the parent view entity.
+   *
+   * @param view  the parent view entity
+   */
+  public void setViewEntity(ViewEntity view) {
+    this.view = view;
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntityPK.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntityPK.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntityPK.java
new file mode 100644
index 0000000..00b65f9
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ViewResourceEntityPK.java
@@ -0,0 +1,99 @@
+/**
+ * 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.server.orm.entities;
+
+import javax.persistence.Column;
+import javax.persistence.Id;
+
+/**
+ * Composite primary key for ViewResourceEntity.
+ */
+public class ViewResourceEntityPK {
+  /**
+   * The view name.
+   */
+  @Id
+  @Column(name = "view_name", nullable = false, insertable = true, updatable = false, length = 100)
+  private String viewName;
+
+  /**
+   * The resource name.
+   */
+  @Id
+  @Column(name = "name", nullable = false, insertable = true, updatable = false, length = 100)
+  private String name;
+
+
+  // ----- ViewResourceEntityPK ----------------------------------------------
+
+  /**
+   * Get the name of the associated view.
+   *
+   * @return view name
+   */
+  public String getViewName() {
+    return viewName;
+  }
+
+  /**
+   * Set the name of the associated view.
+   *
+   * @param viewName  view name
+   */
+  public void setViewName(String viewName) {
+    this.viewName = viewName;
+  }
+
+  /**
+   * Get the name of the resource.
+   *
+   * @return resource name
+   */
+  public String getName() {
+    return name;
+  }
+
+  /**
+   * Set the name of the resource.
+   *
+   * @param name  resource name
+   */
+  public void setName(String name) {
+    this.name = name;
+  }
+
+
+  // ----- Object overrides --------------------------------------------------
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+
+    ViewResourceEntityPK that = (ViewResourceEntityPK) o;
+
+    return this.viewName.equals(that.viewName) &&
+        this.name.equals(that.name);
+  }
+
+  @Override
+  public int hashCode() {
+    return 31 * viewName.hashCode() + name.hashCode();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
index 3e1d1a9..cc8ae48 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.view;
 
 import org.apache.ambari.server.configuration.ComponentSSLConfiguration;
+import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.view.ResourceProvider;
 import org.apache.ambari.view.URLStreamProvider;
 import org.apache.ambari.view.ViewContext;
@@ -42,7 +43,12 @@ public class ViewContextImpl implements ViewContext {
   /**
    * The associated view definition.
    */
-  private final ViewInstanceDefinition viewInstanceDefinition;
+  private final ViewInstanceEntity viewInstanceEntity;
+
+  /**
+   * The view registry.
+   */
+  private final ViewRegistry viewRegistry;
 
   /**
    * The available stream provider.
@@ -53,13 +59,15 @@ public class ViewContextImpl implements ViewContext {
   // ---- Constructors -------------------------------------------------------
 
   /**
-   * Construct a view context from the given view definition.
+   * Construct a view context from the given view entity.
    *
-   * @param viewInstanceDefinition  the view definition
+   * @param viewInstanceEntity  the view entity
+   * @param viewRegistry        the view registry
    */
-  public ViewContextImpl(ViewInstanceDefinition viewInstanceDefinition) {
-    this.viewInstanceDefinition = viewInstanceDefinition;
-    this.streamProvider         = ViewURLStreamProvider.getProvider();
+  public ViewContextImpl(ViewInstanceEntity viewInstanceEntity, ViewRegistry viewRegistry) {
+    this.viewInstanceEntity = viewInstanceEntity;
+    this.viewRegistry       = viewRegistry;
+    this.streamProvider     = ViewURLStreamProvider.getProvider();
   }
 
 
@@ -67,27 +75,49 @@ public class ViewContextImpl implements ViewContext {
 
   @Override
   public String getViewName() {
-    return viewInstanceDefinition.getViewDefinition().getName();
+    return viewInstanceEntity.getViewName();
   }
 
   @Override
   public String getInstanceName() {
-    return viewInstanceDefinition.getName();
+    return viewInstanceEntity.getName();
   }
 
   @Override
   public Map<String, String> getProperties() {
-    return viewInstanceDefinition.getProperties();
+    return Collections.unmodifiableMap(viewInstanceEntity.getPropertyMap());
+  }
+
+  @Override
+  public void putInstanceData(String key, String value) {
+    viewInstanceEntity.putInstanceData(key, value);
+    viewRegistry.updateViewInstance(viewInstanceEntity);
+  }
+
+  @Override
+  public String getInstanceData(String key) {
+    return viewInstanceEntity.getInstanceDataMap().get(key);
+  }
+
+  @Override
+  public Map<String, String> getInstanceData() {
+    return Collections.unmodifiableMap(viewInstanceEntity.getInstanceDataMap());
+  }
+
+  @Override
+  public void removeInstanceData(String key) {
+    viewInstanceEntity.removeInstanceData(key);
+    viewRegistry.updateViewInstance(viewInstanceEntity);
   }
 
   @Override
   public String getAmbariProperty(String key) {
-    return viewInstanceDefinition.getViewDefinition().getAmbariProperty(key);
+    return viewInstanceEntity.getViewEntity().getAmbariProperty(key);
   }
 
   @Override
   public ResourceProvider<?> getResourceProvider(String type) {
-    return viewInstanceDefinition.getResourceProvider(type);
+    return viewInstanceEntity.getResourceProvider(type);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDefinition.java
deleted file mode 100644
index cc2ec5d..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewDefinition.java
+++ /dev/null
@@ -1,297 +0,0 @@
-/**
- * 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.server.view;
-
-import org.apache.ambari.server.configuration.Configuration;
-import org.apache.ambari.server.view.configuration.ResourceConfig;
-import org.apache.ambari.server.view.configuration.ViewConfig;
-import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.controller.spi.ResourceProvider;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Provides access to the attributes of a view.
- */
-public class ViewDefinition {
-  /**
-   * The associated view configuration.
-   */
-  private final ViewConfig configuration;
-
-  /**
-   * The mapping of resource type to resource provider.
-   */
-  private final Map<Resource.Type, ResourceProvider> resourceProviders = new HashMap<Resource.Type, ResourceProvider>();
-
-  /**
-   * The mapping of resource type to resource definition.
-   */
-  private final Map<Resource.Type, ViewSubResourceDefinition> resourceDefinitions = new HashMap<Resource.Type, ViewSubResourceDefinition>();
-
-  /**
-   * The mapping of resource type to resource configuration.
-   */
-  private final Map<Resource.Type, ResourceConfig> resourceConfigurations = new HashMap<Resource.Type, ResourceConfig>();
-
-  /**
-   * The mapping of instance name to instance definition.
-   */
-  private final Map<String, ViewInstanceDefinition> instanceDefinitions = new HashMap<String, ViewInstanceDefinition>();
-
-  /**
-   * The Ambari configuration properties.
-   */
-  private final Configuration ambariConfiguration;
-
-  /**
-   * The external resource type for the view.
-   */
-  private final Resource.Type externalResourceType;
-
-  /**
-   * The classloader used to load the view.
-   */
-  private final ClassLoader classLoader;
-
-  /**
-   * The archive path for the view.
-   */
-  private final String archivePath;
-
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Construct a view definition from the given configuration.
-   *
-   * @param configuration        the view configuration
-   * @param ambariConfiguration  the Ambari configuration
-   * @param classLoader          the class loader
-   * @param archivePath          the path of the view archive
-   */
-  public ViewDefinition(ViewConfig configuration, Configuration ambariConfiguration,
-                        ClassLoader classLoader, String archivePath) {
-    this.configuration       = configuration;
-    this.ambariConfiguration = ambariConfiguration;
-    this.classLoader         = classLoader;
-    this.archivePath         = archivePath;
-
-    this.externalResourceType =
-        new Resource.Type(getQualifiedResourceTypeName(ResourceConfig.EXTERNAL_RESOURCE_PLURAL_NAME));
-  }
-
-
-  // ----- ViewDefinition ----------------------------------------------------
-
-  /**
-   * Get the view name.
-   *
-   * @return the view name
-   */
-  public String getName() {
-    return configuration.getName();
-  }
-
-  /**
-   * Get the view label.
-   *
-   * @return the view label
-   */
-  public String getLabel() {
-    return configuration.getLabel();
-  }
-
-  /**
-   * Get the view version.
-   *
-   * @return the version
-   */
-  public String getVersion() {
-    return configuration.getVersion();
-  }
-
-  /**
-   * Get the associated view configuration.
-   *
-   * @return the configuration
-   */
-  public ViewConfig getConfiguration() {
-    return configuration;
-  }
-
-  /**
-   * Get a property for the given key from the ambari configuration.
-   *
-   * @param key  the property key
-   *
-   * @return the property value; null indicates that the configuration contains no mapping for the key
-   */
-  public String getAmbariProperty(String key) {
-    return ambariConfiguration.getProperty(key);
-  }
-
-  /**
-   * Add a resource provider for the given type.
-   *
-   * @param type      the resource type
-   * @param provider  the resource provider
-   */
-  public void addResourceProvider(Resource.Type type, ResourceProvider provider) {
-    resourceProviders.put(type, provider);
-  }
-
-  /**
-   * Get the resource provider for the given type.
-   *
-   * @param type  the resource type
-   *
-   * @return the resource provider associated with the given type
-   */
-  public ResourceProvider getResourceProvider(Resource.Type type) {
-    return resourceProviders.get(type);
-  }
-
-  /**
-   * Add a resource definition.
-   *
-   * @param definition  the resource definition
-   */
-  public void addResourceDefinition(ViewSubResourceDefinition definition) {
-    resourceDefinitions.put(definition.getType(), definition);
-  }
-
-  /**
-   * Get the resource definition for the given type.
-   *
-   * @param type  the resource type
-   *
-   * @return the resource definition associated with the given type
-   */
-  public ViewSubResourceDefinition getResourceDefinition(Resource.Type type) {
-    return resourceDefinitions.get(type);
-  }
-
-  /**
-   * Get the mapping of resource type to resource definitions.
-   *
-   * @return the mapping of resource type to resource definitions
-   */
-  public Map<Resource.Type, ViewSubResourceDefinition> getResourceDefinitions() {
-    return resourceDefinitions;
-  }
-
-  /**
-   * Add a resource configuration for the given type.
-   *
-   * @param type    the resource type
-   * @param config  the configuration
-   */
-  public void addResourceConfiguration(Resource.Type type, ResourceConfig config) {
-    resourceConfigurations.put(type, config);
-  }
-
-  /**
-   * Get the mapping of resource type to resource configurations.
-   *
-   * @return the mapping of resource types to resource configurations
-   */
-  public Map<Resource.Type, ResourceConfig> getResourceConfigurations() {
-    return resourceConfigurations;
-  }
-
-  /**
-   * Get the set of resource types for this view.
-   *
-   * @return the set of resource type
-   */
-  public Set<Resource.Type> getViewResourceTypes() {
-    return resourceProviders.keySet();
-  }
-
-  /**
-   * Add an instance definition.
-   *
-   * @param viewInstanceDefinition  the instance definition
-   */
-  public void addInstanceDefinition(ViewInstanceDefinition viewInstanceDefinition) {
-    instanceDefinitions.put(viewInstanceDefinition.getName(), viewInstanceDefinition);
-  }
-
-  /**
-   * Get the collection of all instance definitions for this view.
-   *
-   * @return the collection of instance definitions
-   */
-  public Collection<ViewInstanceDefinition> getInstanceDefinitions() {
-    return instanceDefinitions.values();
-  }
-
-  /**
-   * Get an instance definition for the given name.
-   *
-   * @param instanceName  the instance name
-   *
-   * @return the instance definition
-   */
-  public ViewInstanceDefinition getInstanceDefinition(String instanceName) {
-    return instanceDefinitions.get(instanceName);
-  }
-
-  /**
-   * Get the external resource type for the view.
-   *
-   * @return the external resource type
-   */
-  public Resource.Type getExternalResourceType() {
-    return externalResourceType;
-  }
-
-  /**
-   * Get a resource name qualified by the associated view name.
-   *
-   * @param resourceTypeName  the resource type name
-   *
-   * @return the qualified resource name
-   */
-  public String getQualifiedResourceTypeName(String resourceTypeName) {
-    return configuration.getName() + "/" + resourceTypeName;
-  }
-
-  /**
-   * Get the class loader used to load the view classes.
-   *
-   * @return the class loader
-   */
-  public ClassLoader getClassLoader() {
-    return classLoader;
-  }
-
-  /**
-   * Get the path for the archive for the view.
-   *
-   * @return the archive path
-   */
-  public String getArchivePath() {
-    return archivePath;
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExternalSubResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExternalSubResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExternalSubResourceProvider.java
index d3371b6..e0af2ef 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExternalSubResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExternalSubResourceProvider.java
@@ -29,6 +29,8 @@ import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
+import org.apache.ambari.server.orm.entities.ViewEntity;
+import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -67,7 +69,7 @@ public class ViewExternalSubResourceProvider extends AbstractResourceProvider {
   /**
    * The associated view definition.
    */
-  private final ViewDefinition viewDefinition;
+  private final ViewEntity viewDefinition;
 
 
   // ----- Constructors ------------------------------------------------------
@@ -78,7 +80,7 @@ public class ViewExternalSubResourceProvider extends AbstractResourceProvider {
    * @param type            the resource type
    * @param viewDefinition  the associated view definition
    */
-  public ViewExternalSubResourceProvider(Resource.Type type, ViewDefinition viewDefinition) {
+  public ViewExternalSubResourceProvider(Resource.Type type, ViewEntity viewDefinition) {
     super(_getPropertyIds(), _getKeyPropertyIds(type));
 
     this.type           = type;
@@ -100,12 +102,12 @@ public class ViewExternalSubResourceProvider extends AbstractResourceProvider {
 
     Set<Resource> resourceSet = new HashSet<Resource>();
 
-    Set<ViewInstanceDefinition> instanceDefinitions = new HashSet<ViewInstanceDefinition>();
+    Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>();
 
     Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
     int size = propertyMaps.size();
 
-    Collection<ViewInstanceDefinition> viewInstanceDefinitions = viewDefinition.getInstanceDefinitions();
+    Collection<ViewInstanceEntity> viewInstanceDefinitions = viewDefinition.getInstances();
     if (size == 0) {
       instanceDefinitions.addAll(viewInstanceDefinitions);
     } else {
@@ -120,7 +122,7 @@ public class ViewExternalSubResourceProvider extends AbstractResourceProvider {
       }
     }
 
-    for (ViewInstanceDefinition viewInstanceDefinition : instanceDefinitions) {
+    for (ViewInstanceEntity viewInstanceDefinition : instanceDefinitions) {
       for (String resourceName : resourceNames) {
         ResourceImpl resource = new ResourceImpl(type);
         resource.setProperty(VIEW_NAME_PROPERTY_ID, viewDefinition.getName());

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewInstanceDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewInstanceDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewInstanceDefinition.java
deleted file mode 100644
index 42b4f4e..0000000
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewInstanceDefinition.java
+++ /dev/null
@@ -1,193 +0,0 @@
-/**
- * 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.server.view;
-
-import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.view.configuration.InstanceConfig;
-import org.apache.ambari.view.ResourceProvider;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Provides access to the attributes of a view instance.
- */
-public class ViewInstanceDefinition {
-  /**
-   * The prefix for every view instance context path.
-   */
-  public static final String VIEWS_CONTEXT_PATH_PREFIX = "/views/";
-
-  /**
-   * The associated configuration.
-   */
-  private final InstanceConfig instanceConfig;
-
-  /**
-   * The parent view definition.
-   */
-  private final ViewDefinition viewDefinition;
-
-  /**
-   * The view instance properties.
-   */
-  private final Map<String, String> properties = new HashMap<String, String>();
-
-  /**
-   * The mapping of resource type to resource provider.
-   */
-  private final Map<Resource.Type, ResourceProvider> resourceProviders = new HashMap<Resource.Type, ResourceProvider>();
-
-  /**
-   * The mapping of the resource plural name to service.
-   */
-  private final Map<String, Object> services = new HashMap<String, Object>();
-
-  /**
-   * The context path for the view web app.
-   */
-  private final String contextPath;
-
-
-  // ----- Constructors ------------------------------------------------------
-
-  /**
-   * Construct a view instance definition.
-   *
-   * @param viewDefinition  the parent view definition
-   * @param instanceConfig  the associated configuration
-   */
-  public ViewInstanceDefinition(ViewDefinition viewDefinition, InstanceConfig instanceConfig) {
-    this.instanceConfig = instanceConfig;
-    this.viewDefinition = viewDefinition;
-    this.contextPath    = VIEWS_CONTEXT_PATH_PREFIX + viewDefinition.getName() + "/" + instanceConfig.getName();
-  }
-
-
-  // ----- ViewInstanceDefinition --------------------------------------------
-
-  /**
-   * Get the parent view definition.
-   *
-   * @return the parent view definition
-   */
-  public ViewDefinition getViewDefinition() {
-    return viewDefinition;
-  }
-
-  /**
-   * Get the associated configuration.
-   *
-   * @return the configuration
-   */
-  public InstanceConfig getConfiguration() {
-    return instanceConfig;
-  }
-
-  /**
-   * Get the view instance name.
-   *
-   * @return the instance name
-   */
-  public String getName() {
-    return instanceConfig.getName();
-  }
-
-  /**
-   * Add a view instance property.
-   *
-   * @param key    the property key
-   * @param value  the property value
-   */
-  public void addProperty(String key, String value) {
-    properties.put(key, value);
-  }
-
-  /**
-   * Get the view instance properties.
-   *
-   * @return the view instance properties
-   */
-  public Map<String, String> getProperties() {
-    return properties;
-  }
-
-  /**
-   * Add a service for the given plural resource name.
-   *
-   * @param pluralName  the plural resource name
-   * @param service     the service
-   */
-  public void addService(String pluralName, Object service) {
-    services.put(pluralName, service);
-  }
-
-  /**
-   * Get the service associated with the given plural resource name.
-   *
-   * @param pluralName  the plural resource name
-   *
-   * @return the service associated with the given name
-   */
-  public Object getService(String pluralName) {
-    return services.get(pluralName);
-  }
-
-  /**
-   * Add a resource provider for the given resource type.
-   *
-   * @param type      the resource type
-   * @param provider  the resource provider
-   */
-  public void addResourceProvider(Resource.Type type, ResourceProvider provider) {
-    resourceProviders.put(type, provider);
-  }
-
-  /**
-   * Get the resource provider for the given resource type.
-   *
-   * @param type  the resource type
-   *
-   * @return the resource provider
-   */
-  public ResourceProvider getResourceProvider(Resource.Type type) {
-    return resourceProviders.get(type);
-  }
-
-  /**
-   * Get the resource provider for the given resource type name (scoped to this view).
-   *
-   * @param type  the resource type name
-   *
-   * @return the resource provider
-   */
-  public ResourceProvider getResourceProvider(String type) {
-    String typeName = viewDefinition.getName() + "/" + type;
-    return resourceProviders.get(Resource.Type.valueOf(typeName));
-  }
-
-  /**
-   * Get the context path for the UI for this view.
-   *
-   * @return the context path
-   */
-  public String getContextPath() {
-    return contextPath;
-  }
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewProviderModule.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewProviderModule.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewProviderModule.java
index 9cb889a..edaff1b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewProviderModule.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewProviderModule.java
@@ -22,6 +22,7 @@ import org.apache.ambari.server.controller.spi.PropertyProvider;
 import org.apache.ambari.server.controller.spi.ProviderModule;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
+import org.apache.ambari.server.orm.entities.ViewEntity;
 
 import java.util.HashMap;
 import java.util.List;
@@ -89,7 +90,7 @@ public class ViewProviderModule implements ProviderModule {
     Map<Resource.Type, ResourceProvider> resourceProviders = new HashMap<Resource.Type, ResourceProvider>();
 
     ViewRegistry registry = ViewRegistry.getInstance();
-    for (ViewDefinition definition : registry.getDefinitions()) {
+    for (ViewEntity definition : registry.getDefinitions()) {
       for (Resource.Type type : definition.getViewResourceTypes()){
         ResourceProvider provider = definition.getResourceProvider(type);
         resourceProviders.put(type, provider);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
index abeff05..b91cb99 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java
@@ -20,6 +20,7 @@ package org.apache.ambari.server.view;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
+import com.google.inject.Inject;
 import com.google.inject.Injector;
 import org.apache.ambari.server.api.resources.ResourceInstanceFactoryImpl;
 import org.apache.ambari.server.api.resources.SubResourceDefinition;
@@ -28,7 +29,14 @@ import org.apache.ambari.server.api.services.ViewExternalSubResourceService;
 import org.apache.ambari.server.api.services.ViewSubResourceService;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.orm.dao.ViewDAO;
+import org.apache.ambari.server.orm.dao.ViewInstanceDAO;
+import org.apache.ambari.server.orm.entities.ViewEntity;
+import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
+import org.apache.ambari.server.orm.entities.ViewParameterEntity;
+import org.apache.ambari.server.orm.entities.ViewResourceEntity;
 import org.apache.ambari.server.view.configuration.InstanceConfig;
+import org.apache.ambari.server.view.configuration.ParameterConfig;
 import org.apache.ambari.server.view.configuration.PropertyConfig;
 import org.apache.ambari.server.view.configuration.ResourceConfig;
 import org.apache.ambari.server.view.configuration.ViewConfig;
@@ -65,12 +73,12 @@ public class ViewRegistry {
   /**
    * Mapping of view names to view definitions.
    */
-  private Map<String, ViewDefinition> viewDefinitions = new HashMap<String, ViewDefinition>();
+  private Map<String, ViewEntity> viewDefinitions = new HashMap<String, ViewEntity>();
 
   /**
    * Mapping of view instances to view definition and instance name.
    */
-  private Map<ViewDefinition, Map<String, ViewInstanceDefinition>> viewInstanceDefinitions = new HashMap<ViewDefinition, Map<String, ViewInstanceDefinition>>();
+  private Map<ViewEntity, Map<String, ViewInstanceEntity>> viewInstanceDefinitions = new HashMap<ViewEntity, Map<String, ViewInstanceEntity>>();
 
   /**
    * Mapping of view names to sub-resources.
@@ -87,6 +95,28 @@ public class ViewRegistry {
    */
   protected final static Logger LOG = LoggerFactory.getLogger(ViewRegistry.class);
 
+  /**
+   * View data access object.
+   */
+  private static ViewDAO viewDAO;
+
+  /**
+   * View instance data access object.
+   */
+  private static ViewInstanceDAO instanceDAO;
+
+  /**
+   * Static initialization of DAO.
+   *
+   * @param vDAO  view data access object
+   * @param iDAO  view instance data access object
+   */
+  @Inject
+  public static void init(ViewDAO vDAO, ViewInstanceDAO iDAO) {
+    viewDAO     = vDAO;
+    instanceDAO = iDAO;
+  }
+
 
   // ----- Constructors ------------------------------------------------------
 
@@ -104,7 +134,7 @@ public class ViewRegistry {
    *
    * @return the collection of view definitions
    */
-  public Collection<ViewDefinition> getDefinitions() {
+  public Collection<ViewEntity> getDefinitions() {
     return viewDefinitions.values();
   }
 
@@ -115,7 +145,7 @@ public class ViewRegistry {
    *
    * @return the view definition for the given name
    */
-  public ViewDefinition getDefinition(String viewName) {
+  public ViewEntity getDefinition(String viewName) {
     return viewDefinitions.get(viewName);
   }
 
@@ -124,7 +154,7 @@ public class ViewRegistry {
    *
    * @param definition  the definition
    */
-  public void addDefinition(ViewDefinition definition) {
+  public void addDefinition(ViewEntity definition) {
     viewDefinitions.put(definition.getName(), definition);
   }
 
@@ -135,7 +165,7 @@ public class ViewRegistry {
    *
    * @return the collection of view instances for the view definition
    */
-  public Collection<ViewInstanceDefinition> getInstanceDefinitions(ViewDefinition definition) {
+  public Collection<ViewInstanceEntity> getInstanceDefinitions(ViewEntity definition) {
     return definition == null ? null : viewInstanceDefinitions.get(definition).values();
   }
 
@@ -147,8 +177,8 @@ public class ViewRegistry {
    *
    * @return the view instance definition for the given view and instance name
    */
-  public ViewInstanceDefinition getInstanceDefinition(String viewName, String instanceName) {
-    Map<String, ViewInstanceDefinition> viewInstanceDefinitionMap =
+  public ViewInstanceEntity getInstanceDefinition(String viewName, String instanceName) {
+    Map<String, ViewInstanceEntity> viewInstanceDefinitionMap =
         viewInstanceDefinitions.get(getDefinition(viewName));
 
     return viewInstanceDefinitionMap == null ? null : viewInstanceDefinitionMap.get(instanceName);
@@ -160,16 +190,29 @@ public class ViewRegistry {
    * @param definition          the owning view definition
    * @param instanceDefinition  the instance definition
    */
-  public void addInstanceDefinition(ViewDefinition definition, ViewInstanceDefinition instanceDefinition) {
-    Map<String, ViewInstanceDefinition> instanceDefinitions = viewInstanceDefinitions.get(definition);
+  public void addInstanceDefinition(ViewEntity definition, ViewInstanceEntity instanceDefinition) {
+    Map<String, ViewInstanceEntity> instanceDefinitions = viewInstanceDefinitions.get(definition);
     if (instanceDefinitions == null) {
-      instanceDefinitions = new HashMap<String, ViewInstanceDefinition>();
+      instanceDefinitions = new HashMap<String, ViewInstanceEntity>();
       viewInstanceDefinitions.put(definition, instanceDefinitions);
     }
     instanceDefinitions.put(instanceDefinition.getName(), instanceDefinition);
   }
 
   /**
+   * Remove an instance definition for the given view definition.
+   *
+   * @param definition    the owning view definition
+   * @param instanceName  the instance name
+   */
+  public void removeInstanceDefinition(ViewEntity definition, String instanceName) {
+    Map<String, ViewInstanceEntity> instanceDefinitions = viewInstanceDefinitions.get(definition);
+    if (instanceDefinitions != null) {
+      instanceDefinitions.remove(instanceName);
+    }
+  }
+
+  /**
    * Get the view registry singleton.
    *
    * @return  the view registry
@@ -191,7 +234,7 @@ public class ViewRegistry {
 
     if (subResourceDefinitions == null) {
       subResourceDefinitions = new HashSet<SubResourceDefinition>();
-      ViewDefinition definition = getDefinition(viewName);
+      ViewEntity definition = getDefinition(viewName);
       if (definition != null) {
         for (Resource.Type type : definition.getViewResourceTypes()) {
           subResourceDefinitions.add(new SubResourceDefinition(type));
@@ -207,12 +250,12 @@ public class ViewRegistry {
    *
    * @param configuration  Ambari configuration
    */
-  public static Set<ViewInstanceDefinition> readViewArchives(Configuration configuration) {
+  public Set<ViewInstanceEntity> readViewArchives(Configuration configuration) {
 
     File   viewDir = configuration.getViewsDir();
     File[] files   = viewDir.listFiles();
 
-    Set<ViewInstanceDefinition> instanceDefinitions = new HashSet<ViewInstanceDefinition>();
+    Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>();
 
     if (files != null) {
       for (final File fileEntry : files) {
@@ -224,78 +267,106 @@ public class ViewRegistry {
             JAXBContext    jaxbContext      = JAXBContext.newInstance(ViewConfig.class);
             Unmarshaller   jaxbUnmarshaller = jaxbContext.createUnmarshaller();
             ViewConfig     viewConfig       = (ViewConfig) jaxbUnmarshaller.unmarshal(configStream);
-            ViewDefinition viewDefinition   = installView(viewConfig, configuration, cl, fileEntry.getAbsolutePath());
+            ViewEntity viewDefinition       = installView(viewConfig, configuration, cl, fileEntry.getAbsolutePath());
+
+            for (InstanceConfig instanceConfig : viewConfig.getInstances()) {
+              ViewInstanceEntity viewInstanceDefinition = new ViewInstanceEntity(viewDefinition, instanceConfig);
 
-            List<InstanceConfig> instances = viewConfig.getInstances();
+              for (PropertyConfig propertyConfig : instanceConfig.getProperties()) {
+                viewInstanceDefinition.putProperty(propertyConfig.getKey(), propertyConfig.getValue());
+              }
 
-            for (InstanceConfig instanceConfig : instances) {
-              instanceDefinitions.add(installViewInstance(viewDefinition, instanceConfig));
+              _installViewInstance(viewDefinition, viewInstanceDefinition);
+              instanceDefinitions.add(viewInstanceDefinition);
             }
           } catch (Exception e) {
             LOG.error("Caught exception loading view from " + fileEntry.getAbsolutePath(), e);
           }
         }
       }
+      try {
+
+        instanceDefinitions.addAll(persistViews());
+      } catch (ClassNotFoundException e) {
+        LOG.error("Caught exception persisting views.", e);
+      }
     }
     return instanceDefinitions;
   }
 
   /**
-   * Install a view instance described by the given instance configuration
-   * for the view defined by the given view definition.
-   *
-   * @param viewDefinition  the view definition
-   * @param instanceConfig  the instance configuration
-   *
-   * @return the new view instance definition
+   * Install a view instance for the view with the given view name.
    *
-   * @throws ClassNotFoundException if the view classes in the given configuration can not be found
+   * @param instanceEntity  the view instance entity
    */
-  public static ViewInstanceDefinition installViewInstance(ViewDefinition viewDefinition,
-                                                            InstanceConfig instanceConfig)
-      throws ClassNotFoundException {
-
-    ViewInstanceDefinition viewInstanceDefinition = new ViewInstanceDefinition(viewDefinition, instanceConfig);
+  public void installViewInstance(ViewInstanceEntity instanceEntity){
+    String viewName       = instanceEntity.getViewName();
+    ViewEntity viewEntity = getDefinition(viewName);
 
-    List<PropertyConfig> propertyConfigs = instanceConfig.getProperties();
+    if (viewEntity != null) {
+      String instanceName = instanceEntity.getName();
 
-    for (PropertyConfig propertyConfig : propertyConfigs) {
-      viewInstanceDefinition.addProperty(propertyConfig.getKey(), propertyConfig.getValue());
+      if (getInstanceDefinition(viewName, instanceName) == null) {
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("Creating view instance " + viewName + "/" + instanceName);
+        }
+        instanceDAO.create(instanceEntity);
+        try {
+          _installViewInstance(viewEntity, instanceEntity);
+        } catch (ClassNotFoundException e) {
+          LOG.error("Caught exception installing view instance.", e);
+        }
+      }
     }
+  }
 
-    ViewContext viewInstanceContext = new ViewContextImpl(viewInstanceDefinition);
-
-    ViewExternalSubResourceService externalSubResourceService =
-        new ViewExternalSubResourceService(viewDefinition.getExternalResourceType(), viewInstanceDefinition);
-
-    viewInstanceDefinition.addService(ResourceConfig.EXTERNAL_RESOURCE_PLURAL_NAME, externalSubResourceService);
+  /**
+   * Update a view instance for the view with the given view name.
+   *
+   * @param instanceEntity  the view instance entity
+   */
+  public void updateViewInstance(ViewInstanceEntity instanceEntity) {
+    String       viewName   = instanceEntity.getViewName();
+    ViewEntity   viewEntity = getDefinition(viewName);
+
+    if (viewEntity != null) {
+      String instanceName = instanceEntity.getName();
+      ViewInstanceEntity entity = getInstanceDefinition(viewName, instanceName);
+      if (entity != null) {
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("Updating view instance " + viewName + "/" + instanceName);
+        }
 
-    Collection<ViewSubResourceDefinition> resourceDefinitions = viewDefinition.getResourceDefinitions().values();
-    for (ViewSubResourceDefinition resourceDefinition : resourceDefinitions) {
+        entity.setProperties(instanceEntity.getProperties());
+        entity.setData(instanceEntity.getData());
 
-      Resource.Type  type           = resourceDefinition.getType();
-      ResourceConfig resourceConfig = resourceDefinition.getResourceConfiguration();
+        instanceDAO.merge(entity);
+      }
+    }
+  }
 
-      ViewResourceHandler viewResourceService =
-          new ViewSubResourceService(type, viewDefinition.getName(), instanceConfig.getName());
+  /**
+   * Uninstall a view instance for the view with the given view name.
+   *
+   * @param instanceEntity  the view instance entity
+   */
+  public void uninstallViewInstance(ViewInstanceEntity instanceEntity) {
 
-      ClassLoader cl = viewDefinition.getClassLoader();
+    String       viewName   = instanceEntity.getViewName();
+    ViewEntity   viewEntity = getDefinition(viewName);
 
-      Object service = getService(resourceConfig.getServiceClass(cl), viewResourceService, viewInstanceContext);
+    if (viewEntity != null) {
+      String instanceName = instanceEntity.getName();
+      if (getInstanceDefinition(viewName, instanceName) != null) {
 
-      if (resourceConfig.isExternal()) {
-        externalSubResourceService.addResourceService(resourceConfig.getName(), service);
-      } else {
-        viewInstanceDefinition.addService(viewDefinition.getResourceDefinition(type).getPluralName(),service);
-        viewInstanceDefinition.addResourceProvider(type,
-            getProvider(resourceConfig.getProviderClass(cl), viewInstanceContext));
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("Deleting view instance " + viewName + "/" + instanceName);
+        }
+        instanceDAO.remove(instanceEntity);
+        viewEntity.removeInstanceDefinition(instanceName);
+        removeInstanceDefinition(viewEntity, instanceName);
       }
     }
-
-    viewDefinition.addInstanceDefinition(viewInstanceDefinition);
-    ViewRegistry.getInstance().addInstanceDefinition(viewDefinition, viewInstanceDefinition);
-
-    return viewInstanceDefinition;
   }
 
   /**
@@ -305,12 +376,12 @@ public class ViewRegistry {
    *
    * @return a web app context
    */
-  public static WebAppContext getWebAppContext(ViewInstanceDefinition viewInstanceDefinition) {
-    ViewDefinition viewDefinition = viewInstanceDefinition.getViewDefinition();
+  public WebAppContext getWebAppContext(ViewInstanceEntity viewInstanceDefinition) {
+    ViewEntity viewDefinition = viewInstanceDefinition.getViewEntity();
 
-    WebAppContext context = new WebAppContext(viewDefinition.getArchivePath(), viewInstanceDefinition.getContextPath());
+    WebAppContext context = new WebAppContext(viewDefinition.getArchive(), viewInstanceDefinition.getContextPath());
     context.setClassLoader(viewDefinition.getClassLoader());
-    context.setAttribute(ViewContext.CONTEXT_ATTRIBUTE, new ViewContextImpl(viewInstanceDefinition));
+    context.setAttribute(ViewContext.CONTEXT_ATTRIBUTE, new ViewContextImpl(viewInstanceDefinition, this));
     return context;
   }
 
@@ -327,13 +398,28 @@ public class ViewRegistry {
   }
 
   // install a new view definition
-  private static ViewDefinition installView(ViewConfig viewConfig, Configuration ambariConfig,
+  private ViewEntity installView(ViewConfig viewConfig, Configuration ambariConfig,
                                             ClassLoader cl, String archivePath)
       throws ClassNotFoundException, IntrospectionException {
 
-    List<ResourceConfig> resourceConfigurations = viewConfig.getResources();
+    ViewEntity viewDefinition = new ViewEntity(viewConfig, ambariConfig, cl, archivePath);
+
+    List<ParameterConfig> parameterConfigurations = viewConfig.getParameters();
+
+    Collection<ViewParameterEntity> parameters = new HashSet<ViewParameterEntity>();
+    for (ParameterConfig parameterConfiguration : parameterConfigurations) {
+      ViewParameterEntity viewParameterEntity =  new ViewParameterEntity();
 
-    ViewDefinition viewDefinition = new ViewDefinition(viewConfig, ambariConfig, cl, archivePath);
+      viewParameterEntity.setViewName(viewDefinition.getName());
+      viewParameterEntity.setName(parameterConfiguration.getName());
+      viewParameterEntity.setDescription(parameterConfiguration.getDescription());
+      viewParameterEntity.setRequired(parameterConfiguration.isRequired());
+      viewParameterEntity.setViewEntity(viewDefinition);
+      parameters.add(viewParameterEntity);
+    }
+    viewDefinition.setParameters(parameters);
+
+    List<ResourceConfig> resourceConfigurations = viewConfig.getResources();
 
     Resource.Type externalResourceType = viewDefinition.getExternalResourceType();
 
@@ -344,7 +430,19 @@ public class ViewRegistry {
     ResourceInstanceFactoryImpl.addResourceDefinition(externalResourceType,
         new ViewExternalSubResourceDefinition(externalResourceType));
 
+    Collection<ViewResourceEntity> resources = new HashSet<ViewResourceEntity>();
     for (ResourceConfig resourceConfiguration : resourceConfigurations) {
+      ViewResourceEntity viewResourceEntity = new ViewResourceEntity();
+
+      viewResourceEntity.setViewName(viewDefinition.getName());
+      viewResourceEntity.setName(resourceConfiguration.getName());
+      viewResourceEntity.setPluralName(resourceConfiguration.getPluralName());
+      viewResourceEntity.setIdProperty(resourceConfiguration.getIdProperty());
+      viewResourceEntity.setResource(resourceConfiguration.getResource());
+      viewResourceEntity.setService(resourceConfiguration.getService());
+      viewResourceEntity.setProvider(resourceConfiguration.getProvider());
+      viewResourceEntity.setSubResourceNames(resourceConfiguration.getSubResourceNames());
+      viewResourceEntity.setViewEntity(viewDefinition);
 
       ViewSubResourceDefinition resourceDefinition = new ViewSubResourceDefinition(viewDefinition, resourceConfiguration);
       viewDefinition.addResourceDefinition(resourceDefinition);
@@ -361,13 +459,53 @@ public class ViewRegistry {
         String   idProperty = resourceConfiguration.getIdProperty();
 
         viewDefinition.addResourceProvider(type, new ViewSubResourceProvider(type, clazz, idProperty, viewDefinition));
+
+        resources.add(viewResourceEntity);
       }
+      viewDefinition.setResources(resources);
     }
-
-    ViewRegistry.getInstance().addDefinition(viewDefinition);
+    addDefinition(viewDefinition);
     return viewDefinition;
   }
 
+  // install a view instance definition
+  private void _installViewInstance(ViewEntity viewDefinition,
+                                           ViewInstanceEntity viewInstanceDefinition)
+      throws ClassNotFoundException {
+
+    ViewContext viewInstanceContext = new ViewContextImpl(viewInstanceDefinition, this);
+
+    ViewExternalSubResourceService externalSubResourceService =
+        new ViewExternalSubResourceService(viewDefinition.getExternalResourceType(), viewInstanceDefinition);
+
+    viewInstanceDefinition.addService(ResourceConfig.EXTERNAL_RESOURCE_PLURAL_NAME, externalSubResourceService);
+
+    Collection<ViewSubResourceDefinition> resourceDefinitions = viewDefinition.getResourceDefinitions().values();
+    for (ViewSubResourceDefinition resourceDefinition : resourceDefinitions) {
+
+      Resource.Type  type           = resourceDefinition.getType();
+      ResourceConfig resourceConfig = resourceDefinition.getResourceConfiguration();
+
+      ViewResourceHandler viewResourceService =
+          new ViewSubResourceService(type, viewDefinition.getName(), viewInstanceDefinition.getName());
+
+      ClassLoader cl = viewDefinition.getClassLoader();
+
+      Object service = getService(resourceConfig.getServiceClass(cl), viewResourceService, viewInstanceContext);
+
+      if (resourceConfig.isExternal()) {
+        externalSubResourceService.addResourceService(resourceConfig.getName(), service);
+      } else {
+        viewInstanceDefinition.addService(viewDefinition.getResourceDefinition(type).getPluralName(),service);
+        viewInstanceDefinition.addResourceProvider(type,
+            getProvider(resourceConfig.getProviderClass(cl), viewInstanceContext));
+      }
+    }
+
+    viewDefinition.addInstanceDefinition(viewInstanceDefinition);
+    addInstanceDefinition(viewDefinition, viewInstanceDefinition);
+  }
+
   // get the given service class from the given class loader; inject a handler and context
   private static <T> T getService(Class<T> clazz,
                                   final ViewResourceHandler viewResourceHandler,
@@ -397,4 +535,45 @@ public class ViewRegistry {
     });
     return viewInstanceInjector.getInstance(clazz);
   }
+
+  // make sure that the views in the ambari db match the registry
+  private Set<ViewInstanceEntity> persistViews() throws ClassNotFoundException {
+
+    Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>();
+    Set<String> persistedViews = new HashSet<String>();
+
+    for (ViewEntity viewEntity : viewDAO.findAll()) {
+      String name = viewEntity.getName();
+      if (!ViewRegistry.getInstance().viewDefinitions.containsKey(name)) {
+
+        System.out.println("removing view " + name);
+        viewDAO.remove(viewEntity);
+      } else {
+        persistedViews.add(name);
+
+        ViewEntity viewDefinition = ViewRegistry.getInstance().viewDefinitions.get(name);
+
+        for (ViewInstanceEntity viewInstanceEntity : viewEntity.getInstances()){
+          if (viewDefinition.getInstanceDefinition(viewInstanceEntity.getName()) == null) {
+            viewInstanceEntity.setViewEntity(viewDefinition);
+            _installViewInstance(viewDefinition, viewInstanceEntity);
+            instanceDefinitions.add(viewInstanceEntity);
+          }
+        }
+      }
+    }
+
+    // persist new views
+    for (ViewEntity definition : viewDefinitions.values() ) {
+      String viewName = definition.getName();
+
+      if (!persistedViews.contains(viewName)) {
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("Creating View " + viewName + ".");
+        }
+        viewDAO.create(definition);
+      }
+    }
+    return instanceDefinitions;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceDefinition.java
index ac7f091..15b389d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceDefinition.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceDefinition.java
@@ -21,6 +21,7 @@ package org.apache.ambari.server.view;
 import org.apache.ambari.server.api.resources.BaseResourceDefinition;
 import org.apache.ambari.server.api.resources.SubResourceDefinition;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.view.configuration.ResourceConfig;
 
 import java.util.HashSet;
@@ -34,7 +35,7 @@ public class ViewSubResourceDefinition extends BaseResourceDefinition {
   /**
    * The associated view definition.
    */
-  private final ViewDefinition viewDefinition;
+  private final ViewEntity viewDefinition;
 
   /**
    * The configuration.
@@ -55,7 +56,7 @@ public class ViewSubResourceDefinition extends BaseResourceDefinition {
    * @param viewDefinition         the view definition
    * @param resourceConfiguration  the resource configuration
    */
-  public ViewSubResourceDefinition(ViewDefinition viewDefinition, ResourceConfig resourceConfiguration) {
+  public ViewSubResourceDefinition(ViewEntity viewDefinition, ResourceConfig resourceConfiguration) {
     super(new Resource.Type(viewDefinition.getQualifiedResourceTypeName(resourceConfiguration.getName())));
 
     this.viewDefinition        = viewDefinition;

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceProvider.java
index 4241e8f..08c0c1d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewSubResourceProvider.java
@@ -31,6 +31,8 @@ import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.SystemException;
 import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.apache.ambari.server.orm.entities.ViewEntity;
+import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.view.ReadRequest;
 
 import java.beans.IntrospectionException;
@@ -55,7 +57,7 @@ public class ViewSubResourceProvider extends AbstractResourceProvider {
   private static final String VIEW_NAME_PROPERTY_ID = "view_name";
   private static final String INSTANCE_NAME_PROPERTY_ID = "instance_name";
 
-  private final ViewDefinition viewDefinition;
+  private final ViewEntity viewDefinition;
   private final String pkField;
   private final Resource.Type type;
   private final Map<String, PropertyDescriptor> descriptorMap;
@@ -75,7 +77,7 @@ public class ViewSubResourceProvider extends AbstractResourceProvider {
    *
    * @throws IntrospectionException if an exception occurs during introspection of the resource bean class
    */
-  public ViewSubResourceProvider(Resource.Type type, Class<?> clazz, String pkField, ViewDefinition viewDefinition)
+  public ViewSubResourceProvider(Resource.Type type, Class<?> clazz, String pkField, ViewEntity viewDefinition)
       throws IntrospectionException {
 
     super(discoverPropertyIds(clazz), getKeyPropertyIds(pkField, type));
@@ -120,14 +122,14 @@ public class ViewSubResourceProvider extends AbstractResourceProvider {
 
     Set<String> requestedIds = getRequestPropertyIds(request, predicate);
 
-    Set<ViewInstanceDefinition> instanceDefinitions = new HashSet<ViewInstanceDefinition>();
+    Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>();
 
     try {
 
       Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
       int size = propertyMaps.size();
 
-      Collection<ViewInstanceDefinition> viewInstanceDefinitions = viewDefinition.getInstanceDefinitions();
+      Collection<ViewInstanceEntity> viewInstanceDefinitions = viewDefinition.getInstances();
       if (size == 0) {
         instanceDefinitions.addAll(viewInstanceDefinitions);
       } else {
@@ -144,14 +146,17 @@ public class ViewSubResourceProvider extends AbstractResourceProvider {
             instanceDefinitions.addAll(viewInstanceDefinitions);
             break;
           } else {
-            instanceDefinitions.add(viewDefinition.getInstanceDefinition(instanceName));
+            ViewInstanceEntity instanceDefinition = viewDefinition.getInstanceDefinition(instanceName);
+            if (instanceDefinition != null) {
+              instanceDefinitions.add(instanceDefinition);
+            }
           }
         }
       }
 
       Set<Resource> results = new HashSet<Resource>();
       ReadRequest readRequest = new ViewReadRequest(requestedIds, predicate == null ? "" : predicate.toString());
-      for (ViewInstanceDefinition instanceDefinition : instanceDefinitions) {
+      for (ViewInstanceEntity instanceDefinition : instanceDefinitions) {
 
         Set<?> beans = instanceDefinition.getResourceProvider(type).getResources(readRequest);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ResourceConfig.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ResourceConfig.java b/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ResourceConfig.java
index 5d23081..1d40eeb 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ResourceConfig.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ResourceConfig.java
@@ -131,6 +131,33 @@ public class ResourceConfig {
   }
 
   /**
+   * Get the provider class name.
+   *
+   * @return the provider class name
+   */
+  public String getProvider() {
+    return provider;
+  }
+
+  /**
+   * Get the service class name.
+   *
+   * @return the service class name.
+   */
+  public String getService() {
+    return service;
+  }
+
+  /**
+   * Get the resource class name
+   *
+   * @return the resource classname
+   */
+  public String getResource() {
+    return resource;
+  }
+
+  /**
    * Get the resource provider class.
    *
    * @param cl  the class loader

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ViewConfig.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ViewConfig.java b/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ViewConfig.java
index 829e08a..8020bc0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ViewConfig.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/configuration/ViewConfig.java
@@ -68,11 +68,6 @@ public class ViewConfig {
   private List<InstanceConfig> instances;
 
   /**
-   * The mapping of servlet names to URL patterns.
-   */
-  public Map<String, String> servletURLPatternMap = null;
-
-  /**
    * Get the unique name.
    *
    * @return the view name

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
index da109cf..44fdb1a 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
@@ -61,6 +61,19 @@ CREATE TABLE hostgroup (blueprint_name VARCHAR(255) NOT NULL, name VARCHAR(255)
 CREATE TABLE hostgroup_component (blueprint_name VARCHAR(255) NOT NULL, hostgroup_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(blueprint_name, hostgroup_name, name));
 CREATE TABLE blueprint_configuration (blueprint_name VARCHAR(255) NOT NULL, type_name VARCHAR(255) NOT NULL, config_data VARCHAR(32000) NOT NULL , PRIMARY KEY(blueprint_name, type_name));
 
+CREATE TABLE ambari.viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255) NOT NULL, version VARCHAR(255), archive VARCHAR(255), PRIMARY KEY(view_name));
+CREATE TABLE ambari.viewinstancedata (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name));
+CREATE TABLE ambari.viewinstance (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, name));
+CREATE TABLE ambari.viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name));
+CREATE TABLE ambari.viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), PRIMARY KEY(view_name, name));
+CREATE TABLE ambari.viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), resource VARCHAR(255), PRIMARY KEY(view_name, name));
+GRANT ALL PRIVILEGES ON TABLE ambari.view TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewinstancedata TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewinstance TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewinstanceproperty TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewparameter TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewresource TO :username;
+
 ALTER TABLE users ADD CONSTRAINT UNQ_users_0 UNIQUE (user_name, ldap_user);
 ALTER TABLE clusterconfig ADD CONSTRAINT FK_clusterconfig_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
 ALTER TABLE clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
@@ -97,6 +110,11 @@ ALTER TABLE hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint
 ALTER TABLE hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES hostgroup(blueprint_name, name);
 ALTER TABLE blueprint_configuration ADD CONSTRAINT FK_cfg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES blueprint(blueprint_name);
 ALTER TABLE requestresourcefilter ADD CONSTRAINT FK_reqresfilter_req_id FOREIGN KEY (request_id) REFERENCES request (request_id);
+ALTER TABLE ambari.viewparameter ADD FOREIGN KEY (view_name) REFERENCES ambari.viewmain(view_name);
+ALTER TABLE ambari.viewresource ADD FOREIGN KEY (view_name) REFERENCES ambari.viewmain(view_name);
+ALTER TABLE ambari.viewinstance ADD FOREIGN KEY (view_name) REFERENCES ambari.viewmain(view_name);
+ALTER TABLE ambari.viewinstanceproperty ADD FOREIGN KEY (view_name, view_instance_name) REFERENCES ambari.viewinstance(view_name, name);
+ALTER TABLE ambari.viewinstancedata ADD FOREIGN KEY (view_name, view_instance_name) REFERENCES ambari.viewinstance(view_name, name);
 
 
 INSERT INTO ambari_sequences(sequence_name, value) values ('cluster_id_seq', 1);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
index 38cc3d7..fbcc219 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
@@ -50,6 +50,12 @@ CREATE TABLE blueprint (blueprint_name VARCHAR2(255) NOT NULL, stack_name VARCHA
 CREATE TABLE hostgroup (blueprint_name VARCHAR2(255) NOT NULL, name VARCHAR2(255) NOT NULL, cardinality VARCHAR2(255) NOT NULL, PRIMARY KEY(blueprint_name, name));
 CREATE TABLE hostgroup_component (blueprint_name VARCHAR2(255) NOT NULL, hostgroup_name VARCHAR2(255) NOT NULL, name VARCHAR2(255) NOT NULL, PRIMARY KEY(blueprint_name, hostgroup_name, name));
 CREATE TABLE blueprint_configuration (blueprint_name VARCHAR2(255) NOT NULL, type_name VARCHAR2(255) NOT NULL, config_data CLOB NOT NULL , PRIMARY KEY(blueprint_name, type_name));
+CREATE TABLE viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255) NOT NULL, version VARCHAR(255), archive VARCHAR(255), PRIMARY KEY(view_name));
+CREATE TABLE viewinstancedata (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name));
+CREATE TABLE viewinstance (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, name));
+CREATE TABLE viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name));
+CREATE TABLE viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), PRIMARY KEY(view_name, name));
+CREATE TABLE viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), resource VARCHAR(255), PRIMARY KEY(view_name, name));
 
 ALTER TABLE users ADD CONSTRAINT UNQ_users_0 UNIQUE (user_name, ldap_user);
 ALTER TABLE clusterconfig ADD CONSTRAINT FK_clusterconfig_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id);
@@ -87,6 +93,11 @@ ALTER TABLE hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (blueprint
 ALTER TABLE hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES hostgroup(blueprint_name, name);
 ALTER TABLE blueprint_configuration ADD CONSTRAINT FK_cfg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES blueprint(blueprint_name);
 ALTER TABLE requestresourcefilter ADD CONSTRAINT FK_reqresfilter_req_id FOREIGN KEY (request_id) REFERENCES request (request_id);
+ALTER TABLE viewparameter ADD CONSTRAINT FK_viewparameter_view_name FOREIGN KEY (view_name) REFERENCES viewmain(view_name);
+ALTER TABLE viewresource ADD CONSTRAINT FK_viewresource_view_name FOREIGN KEY (view_name) REFERENCES viewmain(view_name);
+ALTER TABLE viewinstance ADD CONSTRAINT FK_viewinstance_view_name FOREIGN KEY (view_name) REFERENCES viewmain(view_name);
+ALTER TABLE viewinstanceproperty ADD CONSTRAINT FK_viewinstanceproperty_view_name FOREIGN KEY (view_name, view_instance_name) REFERENCES viewinstance(view_name, name);
+ALTER TABLE viewinstancedata ADD CONSTRAINT FK_viewinstancedata_view_name FOREIGN KEY (view_name, view_instance_name) REFERENCES viewinstance(view_name, name);
 
 INSERT INTO ambari_sequences(sequence_name, value) values ('host_role_command_id_seq', 0);
 INSERT INTO ambari_sequences(sequence_name, value) values ('user_id_seq', 1);

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
index 62129b5..4b96984 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
@@ -127,6 +127,19 @@ GRANT ALL PRIVILEGES ON TABLE ambari.hostgroup TO :username;
 GRANT ALL PRIVILEGES ON TABLE ambari.hostgroup_component TO :username;
 GRANT ALL PRIVILEGES ON TABLE ambari.blueprint_configuration TO :username;
 
+CREATE TABLE ambari.viewmain (view_name VARCHAR(255) NOT NULL, label VARCHAR(255) NOT NULL, version VARCHAR(255), archive VARCHAR(255), PRIMARY KEY(view_name));
+CREATE TABLE ambari.viewinstancedata (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name));
+CREATE TABLE ambari.viewinstance (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, name));
+CREATE TABLE ambari.viewinstanceproperty (view_name VARCHAR(255) NOT NULL, view_instance_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, value VARCHAR(255) NOT NULL, PRIMARY KEY(view_name, view_instance_name, name));
+CREATE TABLE ambari.viewparameter (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(255), required CHAR(1), PRIMARY KEY(view_name, name));
+CREATE TABLE ambari.viewresource (view_name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, plural_name VARCHAR(255), id_property VARCHAR(255), subResource_names VARCHAR(255), provider VARCHAR(255), service VARCHAR(255), resource VARCHAR(255), PRIMARY KEY(view_name, name));
+GRANT ALL PRIVILEGES ON TABLE ambari.viewmain TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewinstancedata TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewinstance TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewinstanceproperty TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewparameter TO :username;
+GRANT ALL PRIVILEGES ON TABLE ambari.viewresource TO :username;
+
 --------altering tables by creating foreign keys----------
 ALTER TABLE ambari.clusterconfig ADD CONSTRAINT FK_clusterconfig_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id);
 ALTER TABLE ambari.clusterservices ADD CONSTRAINT FK_clusterservices_cluster_id FOREIGN KEY (cluster_id) REFERENCES ambari.clusters (cluster_id);
@@ -163,6 +176,11 @@ ALTER TABLE ambari.hostgroup ADD CONSTRAINT FK_hg_blueprint_name FOREIGN KEY (bl
 ALTER TABLE ambari.hostgroup_component ADD CONSTRAINT FK_hgc_blueprint_name FOREIGN KEY (blueprint_name, hostgroup_name) REFERENCES ambari.hostgroup (blueprint_name, name);
 ALTER TABLE ambari.blueprint_configuration ADD CONSTRAINT FK_cfg_blueprint_name FOREIGN KEY (blueprint_name) REFERENCES ambari.blueprint(blueprint_name);
 ALTER TABLE ambari.requestresourcefilter ADD CONSTRAINT FK_reqresfilter_req_id FOREIGN KEY (request_id) REFERENCES ambari.request (request_id);
+ALTER TABLE ambari.viewparameter ADD FOREIGN KEY (view_name) REFERENCES ambari.viewmain(view_name);
+ALTER TABLE ambari.viewresource ADD FOREIGN KEY (view_name) REFERENCES ambari.viewmain(view_name);
+ALTER TABLE ambari.viewinstance ADD FOREIGN KEY (view_name) REFERENCES ambari.viewmain(view_name);
+ALTER TABLE ambari.viewinstanceproperty ADD FOREIGN KEY (view_name, view_instance_name) REFERENCES ambari.viewinstance(view_name, name);
+ALTER TABLE ambari.viewinstancedata ADD FOREIGN KEY (view_name, view_instance_name) REFERENCES ambari.viewinstance(view_name, name);
 
 
 ---------inserting some data-----------

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/META-INF/persistence.xml b/ambari-server/src/main/resources/META-INF/persistence.xml
index 5148e63..50f1fe0 100644
--- a/ambari-server/src/main/resources/META-INF/persistence.xml
+++ b/ambari-server/src/main/resources/META-INF/persistence.xml
@@ -45,6 +45,12 @@
     <class>org.apache.ambari.server.orm.entities.HostGroupEntity</class>
     <class>org.apache.ambari.server.orm.entities.HostGroupComponentEntity</class>
     <class>org.apache.ambari.server.orm.entities.RequestResourceFilterEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ViewEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ViewInstanceDataEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ViewInstanceEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ViewInstancePropertyEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ViewParameterEntity</class>
+    <class>org.apache.ambari.server.orm.entities.ViewResourceEntity</class>
 
     <properties>
       <!--<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/ambari" />-->

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/test/java/org/apache/ambari/server/api/services/ViewExternalSubResourceServiceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ViewExternalSubResourceServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ViewExternalSubResourceServiceTest.java
index 40ae816..155f91e 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/ViewExternalSubResourceServiceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/ViewExternalSubResourceServiceTest.java
@@ -18,8 +18,8 @@
 package org.apache.ambari.server.api.services;
 
 import org.apache.ambari.server.controller.spi.Resource;
-import org.apache.ambari.server.view.ViewInstanceDefinition;
-import org.apache.ambari.server.view.ViewInstanceDefinitionTest;
+import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
+import org.apache.ambari.server.orm.entities.ViewInstanceEntityTest;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -31,7 +31,7 @@ public class ViewExternalSubResourceServiceTest {
   public void testAddResourceService() throws Exception {
     Resource.Type type = new Resource.Type("resource");
 
-    ViewInstanceDefinition definition = ViewInstanceDefinitionTest.getViewInstanceDefinition();
+    ViewInstanceEntity definition = ViewInstanceEntityTest.getViewInstanceEntity();
     ViewExternalSubResourceService service = new ViewExternalSubResourceService(type, definition);
 
     Object fooService = new Object();

http://git-wip-us.apache.org/repos/asf/ambari/blob/7c198ac3/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/ViewEntityTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/ViewEntityTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/ViewEntityTest.java
new file mode 100644
index 0000000..9bcf86a
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/ViewEntityTest.java
@@ -0,0 +1,192 @@
+/**
+ * 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.server.orm.entities;
+
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.ResourceProvider;
+import org.apache.ambari.server.view.ViewSubResourceDefinition;
+import org.apache.ambari.server.view.configuration.ResourceConfig;
+import org.apache.ambari.server.view.configuration.ResourceConfigTest;
+import org.apache.ambari.server.view.configuration.ViewConfig;
+import org.apache.ambari.server.view.configuration.ViewConfigTest;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Properties;
+import java.util.Set;
+
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+
+/**
+ * ViewEntity tests.
+ */
+public class ViewEntityTest {
+
+
+  public static ViewEntity getViewEntity() throws Exception {
+    return getViewEntity(ViewConfigTest.getConfig());
+  }
+
+  public static ViewEntity getViewEntity(ViewConfig viewConfig) throws Exception {
+    Properties properties = new Properties();
+    properties.put("p1", "v1");
+    properties.put("p2", "v2");
+    properties.put("p3", "v3");
+
+    Configuration ambariConfig = new Configuration(properties);
+    return new ViewEntity(viewConfig, ambariConfig, ViewEntityTest.class.getClassLoader(), "view.jar");
+  }
+
+  @Test
+  public void testGetName() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+    Assert.assertEquals("MY_VIEW", viewDefinition.getName());
+  }
+
+  @Test
+  public void testGetLabel() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+    Assert.assertEquals("My View!", viewDefinition.getLabel());
+  }
+
+  @Test
+  public void testGetVersion() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+    Assert.assertEquals("1.0.0", viewDefinition.getVersion());
+  }
+
+  @Test
+  public void testGetConfiguration() throws Exception {
+    ViewConfig viewConfig = ViewConfigTest.getConfig();
+    ViewEntity viewDefinition = getViewEntity(viewConfig);
+    Assert.assertEquals(viewConfig, viewDefinition.getConfiguration());
+  }
+
+  @Test
+  public void testGetAmbariProperty() throws Exception {
+    ViewConfig viewConfig = ViewConfigTest.getConfig();
+    ViewEntity viewDefinition = getViewEntity(viewConfig);
+    Assert.assertEquals("v1", viewDefinition.getAmbariProperty("p1"));
+    Assert.assertEquals("v2", viewDefinition.getAmbariProperty("p2"));
+    Assert.assertEquals("v3", viewDefinition.getAmbariProperty("p3"));
+  }
+
+  @Test
+  public void testAddGetResourceProvider() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+
+    ResourceProvider provider1 = createNiceMock(ResourceProvider.class);
+
+    Resource.Type type1 = new Resource.Type("myType1");
+    viewDefinition.addResourceProvider(type1, provider1);
+
+    Assert.assertEquals(provider1, viewDefinition.getResourceProvider(type1));
+
+    ResourceProvider provider2 = createNiceMock(ResourceProvider.class);
+
+    Resource.Type type2 = new Resource.Type("myType2");
+    viewDefinition.addResourceProvider(type2, provider2);
+
+    Assert.assertEquals(provider2, viewDefinition.getResourceProvider(type2));
+
+    Set<Resource.Type> types = viewDefinition.getViewResourceTypes();
+
+    Assert.assertEquals(2, types.size());
+
+    Assert.assertTrue(types.contains(type1));
+    Assert.assertTrue(types.contains(type2));
+  }
+
+  @Test
+  public void testAddGetResourceDefinition() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+
+    ViewSubResourceDefinition definition = createNiceMock(ViewSubResourceDefinition.class);
+    Resource.Type type = new Resource.Type("myType");
+
+    expect(definition.getType()).andReturn(type);
+
+    replay(definition);
+
+    viewDefinition.addResourceDefinition(definition);
+
+    Assert.assertEquals(definition, viewDefinition.getResourceDefinition(type));
+
+    verify(definition);
+  }
+
+  @Test
+  public void testAddGetResourceConfiguration() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+
+    ResourceConfig config = ResourceConfigTest.getResourceConfigs().get(0);
+
+    Resource.Type type1 = new Resource.Type("myType");
+
+    viewDefinition.addResourceConfiguration(type1, config);
+
+    Assert.assertEquals(config, viewDefinition.getResourceConfigurations().get(type1));
+
+    Resource.Type type2 = new Resource.Type("myType2");
+
+    viewDefinition.addResourceConfiguration(type2, config);
+
+    Assert.assertEquals(config, viewDefinition.getResourceConfigurations().get(type2));
+  }
+
+  @Test
+  public void testAddGetInstanceDefinition() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+
+    ViewInstanceEntity definition = createNiceMock(ViewInstanceEntity.class);
+
+    expect(definition.getName()).andReturn("instance1").anyTimes();
+
+    replay(definition);
+
+    viewDefinition.addInstanceDefinition(definition);
+
+    Assert.assertEquals(definition, viewDefinition.getInstanceDefinition("instance1"));
+
+    Collection<ViewInstanceEntity> definitions = viewDefinition.getInstances();
+
+    Assert.assertEquals(1, definitions.size());
+
+    Assert.assertTrue(definitions.contains(definition));
+
+    verify(definition);
+  }
+
+  @Test
+  public void testGetClassLoader() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+    Assert.assertEquals(ViewEntityTest.class.getClassLoader(), viewDefinition.getClassLoader());
+  }
+
+  @Test
+  public void testGetArchive() throws Exception {
+    ViewEntity viewDefinition = getViewEntity();
+    Assert.assertEquals("view.jar", viewDefinition.getArchive());
+  }
+}


Mime
View raw message