001: /**
002: * $Id: CreateConfigurationBean.java,v 1.7 2006/06/01 18:10:55 sorensen Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.console.ssoa;
014:
015: import java.util.*;
016: import java.io.*;
017: import java.util.logging.Level;
018: import com.sun.data.provider.impl.ObjectListDataProvider;
019: import com.sun.web.ui.model.Option;
020: import com.sun.web.ui.event.WizardEvent;
021: import com.sun.web.ui.event.WizardEventListener;
022:
023: import javax.faces.application.FacesMessage;
024: import javax.faces.component.UIComponent;
025: import javax.faces.context.FacesContext;
026: import javax.faces.event.ActionEvent;
027: import javax.faces.validator.ValidatorException;
028: import javax.management.MBeanServerConnection;
029:
030: public class CreateConfigurationBean extends ListConfigurationsByDnBean {
031:
032: // restart is a switch to see if the wizard needs to be reset next time it is launched
033: private String basis;
034: private String name;
035: // Used to make sure there is no change in dn during name validation and vice-versa
036: private String nameInValidation;
037: private String dnInValidation;
038:
039: private Option[] availableTemplates;
040: private boolean hasService = false;
041:
042: // Fulfil the abstract methods
043: public String gotoConfigurationHome() {
044: return "";
045: }
046:
047: // Make basis manipulate a session attribute. This allows the been to be used from
048: // the list by dn and list by template pages.
049: public String getBasis() {
050: if (basis == null) {
051: // If the basis is null, check first if it is set in the session.
052: basis = (String) getSessionAttribute(ATTR_SELECTED_TEMPLATE);
053: if (basis == null) {
054: // If not, have populateAvailableTemplates set it
055: populateAvailableTemplates();
056: }
057: }
058: return basis;
059: }
060:
061: public void setBasis(String basis) {
062: this .basis = basis;
063: }
064:
065: public String getName() {
066: return name;
067: }
068:
069: public void setName(String name) {
070: this .name = name;
071: }
072:
073: // Populate the available templates if availableTemplates is null
074: // In addition, set basis to the first template should basis be null
075: public void populateAvailableTemplates() {
076: log(Level.FINEST,
077: "CreateConfigurationsBean.populateAvailableTemplates start");
078: // Get available templates
079: MBeanServerConnection conn = getMBeanServerConnection();
080: List tNames = new ArrayList();
081: try {
082: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
083: // ReflectionException, RuntimeOperationsException, or IOException
084: tNames = (List) conn.getAttribute(objectName,
085: "TemplateNames");
086: } catch (Exception ex) {
087: log(
088: Level.SEVERE,
089: "CreateConfigurationsBean.getAvailableTemplates: Exception when trying to get the list of templates",
090: ex);
091: }
092: availableTemplates = new Option[tNames.size()];
093: for (int i = 0; i < tNames.size(); i++) {
094: String tName = (String) tNames.get(i);
095: // If basis isn't set, set it to the first item
096: if ((i == 0) && (basis == null)) {
097: log(Level.FINEST,
098: "CreateConfigurationsBean.getAvailableTempaltes setting basis to "
099: + tName);
100: setBasis(tName);
101: }
102: availableTemplates[i] = new Option(tName, tName);
103: }
104: }
105:
106: // return the available templates
107: public Option[] getAvailableTemplates() {
108: if (availableTemplates == null) {
109: populateAvailableTemplates();
110: }
111: return availableTemplates;
112: }
113:
114: public String create() {
115: try {
116: // Create a new template with these properties
117: String[] signature = { String.class.getName(),
118: String.class.getName(), String.class.getName() };
119: Object[] params = { getCurrentDN(), name, basis };
120: MBeanServerConnection connection = getMBeanServerConnection();
121: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
122: // ReflectionException, RuntimeOperationsException, or IOException
123: connection.invoke(objectName, "createConfiguration",
124: params, signature);
125: } catch (Exception ex) {
126: log(Level.SEVERE,
127: "CreateConfigurationBean.create: Exception when creating new configuration "
128: + name + " at dn " + getCurrentDN(), ex);
129: }
130: // This will tell the configuration list by dn bean to re-query the list from the MBean
131: setSessionAttribute("resetSSOA", "true");
132: return cancel();
133: }
134:
135: //============================================================
136: // validator for unique template name
137: //============================================================
138: public void validateName(FacesContext ctx, UIComponent comp,
139: Object value) {
140: //nameInValidation = (String) value;
141: log(Level.FINEST, "ListConfigurationsBean.validateName start");
142:
143: String sm = (String) rbMap.get("error.validation.summary");
144: String cn = (String) value;
145: // Make sure no illegal characters are used
146: // Allow alphanumerics, dash, and underscore only
147: if (cn != null && cn.matches(".*[^a-zA-Z0-9-_].*")) {
148: String cm = (String) rbMap
149: .get("error.adapter.illegal.characters");
150: throw new ValidatorException(new FacesMessage(sm, cm));
151: }
152:
153: // Make sure this isn't in the list of configurations at the dn
154: if (hasSSOAService(getCurrentDN())) {
155: MBeanServerConnection conn = getMBeanServerConnection();
156: Map configs = new HashMap();
157: try {
158: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
159: // ReflectionException, RuntimeOperationsException, or IOException
160: String[] signature = { String.class.getName() };
161: Object[] params = { getCurrentDN() };
162: MBeanServerConnection connection = getMBeanServerConnection();
163: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
164: // ReflectionException, RuntimeOperationsException, or IOException
165: configs = (Map) connection.invoke(objectName,
166: "getConfigurationNamesByDn", params, signature);
167: } catch (Exception ex) {
168: log(
169: Level.SEVERE,
170: "CreateConfigurationBean.validateName(): Exception when trying to get the list of configurations",
171: ex);
172: configs = new HashMap();
173: }
174: ArrayList configList = new ArrayList(configs.size());
175: Set names = configs.keySet();
176: Iterator iter = names.iterator();
177: while (iter.hasNext()) {
178: String name = (String) iter.next();
179: if (name.equals(cn)) {
180: String dm = (String) rbMap.get("error.duplicate");
181: log(Level.FINEST,
182: "ListConfigurationsBean.validateName end with error");
183: throw new ValidatorException(new FacesMessage(sm,
184: dm));
185: }
186: }
187: }
188: log(Level.FINEST, "ListConfigurationsBean.validateName end");
189: }
190:
191: //============================================================
192: // validator for dn to have ssoadapter service
193: //============================================================
194: public void validateDn(FacesContext ctx, UIComponent comp,
195: Object value) {
196: // Make sure the dn has the ssoadapter service template
197: // Immediately set the current dn to the value in order to validate the name properly
198: log(Level.FINEST,
199: "ListConfigurationsBean.validateDn start: currentDn is "
200: + getCurrentDN() + " and value passed is "
201: + value);
202: setCurrentDN(value);
203: /* try {
204: String[] signature = {String.class.getName()};
205: Object[] params= {value};
206: MBeanServerConnection connection = getMBeanServerConnection();
207: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
208: // ReflectionException, RuntimeOperationsException, or IOException
209: Boolean hasServiceBool = (Boolean) connection.invoke(objectName, "hasSSOAdapterService", params, signature);
210: hasService = hasServiceBool.booleanValue();
211: if (!hasService){
212: log(Level.SEVERE, "CreateConfigurationBean.validateDn: dn "+value+
213: " does not have the SSOAdapter service registered.");
214: }
215: } catch (Exception ex) {
216: log(Level.SEVERE, "CreateConfigurationBean.validateDn: Exception when" +
217: " attempting to check ssoadapter service for dn "+value, ex);
218: hasService = false;
219: }*/
220: if (!hasSSOAService((String) value)) {
221: // Have to set current dn to the value otherwise the dn stays at the previous dn
222: setCurrentDN(value);
223: String sm = (String) rbMap.get("error.validation.summary");
224: String dm = (String) rbMap.get("error.noservice");
225: log(Level.FINEST,
226: "ListConfigurationsBean.validateDn end with error");
227: throw new ValidatorException(new FacesMessage(sm, dm));
228: }
229: // There is no way to force one validator to be called before the other. All that can
230: // be done is to call the name validator on the private variable "nameInValidation"
231: // that is set by the validateName method immediately upon call
232: log(Level.FINEST, "ListConfigurationsBean.validateDn end");
233: }
234:
235: // Reset the bean and return the cancel command
236: public String cancel() {
237: log(Level.FINEST, "CreateConfigurationBean.cancel: Start");
238: // Reset the bean
239: basis = null;
240: name = null;
241: availableTemplates = null;
242: return "cancel";
243: }
244:
245: }
|