001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.admin.ui.actions;
023:
024: import org.jboss.portal.core.admin.ui.InstanceManagerBean;
025: import org.jboss.portal.core.admin.ui.PortletManagerBean;
026: import org.jboss.portal.core.model.instance.Instance;
027: import org.jboss.portal.core.model.instance.InstanceContainer;
028: import org.jboss.portal.portlet.Portlet;
029: import org.jboss.portal.security.RoleSecurityBinding;
030: import org.jboss.portal.security.SecurityConstants;
031: import org.jboss.portal.security.spi.provider.DomainConfigurator;
032:
033: import javax.faces.application.FacesMessage;
034: import javax.faces.context.FacesContext;
035: import java.util.Collections;
036: import java.util.Set;
037:
038: /**
039: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
040: * @version $Revision: 8785 $
041: */
042: public class CreateInstanceAction {
043:
044: /** . */
045: private String instanceId;
046:
047: /** . */
048: private PortletManagerBean portletMgr;
049:
050: /** . */
051: private InstanceManagerBean instanceMgr;
052:
053: public String getInstanceId() {
054: return instanceId;
055: }
056:
057: public void setInstanceId(String instanceId) {
058: this .instanceId = instanceId;
059: }
060:
061: public PortletManagerBean getPortletManager() {
062: return portletMgr;
063: }
064:
065: public void setPortletManager(PortletManagerBean portletManager) {
066: this .portletMgr = portletManager;
067: }
068:
069: public InstanceManagerBean getInstanceManager() {
070: return instanceMgr;
071: }
072:
073: public void setInstanceManager(InstanceManagerBean instanceManager) {
074: this .instanceMgr = instanceManager;
075: }
076:
077: public String execute() {
078: FacesContext faces = FacesContext.getCurrentInstance();
079:
080: // Validate instance id
081: if (instanceId == null) {
082: FacesMessage message = new FacesMessage(
083: FacesMessage.SEVERITY_ERROR, "No instance id!",
084: null);
085: faces.addMessage("add_instance_form:instanceId", message);
086: } else {
087: // Trim name
088: instanceId = instanceId.trim();
089:
090: // Check for empty string
091: if (instanceId.length() == 0) {
092: FacesMessage message = new FacesMessage(
093: FacesMessage.SEVERITY_ERROR,
094: "Instance id cannot be blank!", null);
095: faces.addMessage("add_instance_form:instanceId",
096: message);
097: } else {
098: // Check for duplicate instance
099: Instance instance = instanceMgr.getInstanceContainer()
100: .getDefinition(instanceId);
101: if (instance != null) {
102: FacesMessage message = new FacesMessage(
103: FacesMessage.SEVERITY_ERROR,
104: "An instance with the id " + instanceId
105: + " already exist!", null);
106: faces.addMessage("add_instance_form:instanceId",
107: message);
108: }
109: }
110: }
111:
112: //
113: if (faces.getMessages().hasNext()) {
114: return null;
115: }
116:
117: //
118: Portlet portlet = portletMgr.getSelectedPortlet();
119: if (portlet == null) {
120: FacesMessage message = new FacesMessage(
121: FacesMessage.SEVERITY_ERROR,
122: "No portlet was selected!", null);
123: faces.addMessage("add_instance_form:instanceId", message);
124: return null;
125: } else {
126: InstanceContainer container = instanceMgr
127: .getInstanceContainer();
128: try {
129: Instance instance = container.createDefinition(
130: instanceId, portlet.getContext().getId());
131: instanceMgr.setSelectedId(instanceId);
132:
133: //
134: DomainConfigurator configurator = instanceMgr
135: .getDomainConfigurator();
136: Set constraints = Collections
137: .singleton(new RoleSecurityBinding("view",
138: SecurityConstants.UNCHECKED_ROLE_NAME));
139: configurator.setSecurityBindings(instance.getId(),
140: constraints);
141:
142: // Configure the instance manager with the instance already looked up
143: instanceMgr.refresh();
144: instanceMgr.setSelectedId(instanceId);
145: instanceMgr.setSelectedPlugin("info");
146:
147: //
148: return "instances";
149: } catch (Exception e) {
150: FacesMessage message = new FacesMessage(
151: FacesMessage.SEVERITY_ERROR,
152: "An unexpected error happened!", null);
153: faces.addMessage("add_instance_form:instanceId",
154: message);
155: return null;
156: }
157: }
158: }
159: }
|