Source Code Cross Referenced for SecurityRealmPortlet.java in  » EJB-Server-geronimo » plugins » org » apache » geronimo » console » securitymanager » realm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server geronimo » plugins » org.apache.geronimo.console.securitymanager.realm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */package org.apache.geronimo.console.securitymanager.realm;
0017:
0018:        import java.io.BufferedReader;
0019:        import java.io.File;
0020:        import java.io.FileWriter;
0021:        import java.io.IOException;
0022:        import java.io.PrintWriter;
0023:        import java.io.Serializable;
0024:        import java.io.StringReader;
0025:        import java.io.StringWriter;
0026:        import java.io.Writer;
0027:        import java.net.MalformedURLException;
0028:        import java.net.URI;
0029:        import java.net.URL;
0030:        import java.net.URLClassLoader;
0031:        import java.util.ArrayList;
0032:        import java.util.Arrays;
0033:        import java.util.Collections;
0034:        import java.util.HashMap;
0035:        import java.util.Iterator;
0036:        import java.util.LinkedHashMap;
0037:        import java.util.List;
0038:        import java.util.Map;
0039:        import java.util.Set;
0040:        import java.util.SortedSet;
0041:
0042:        import javax.enterprise.deploy.spi.DeploymentManager;
0043:        import javax.enterprise.deploy.spi.Target;
0044:        import javax.enterprise.deploy.spi.TargetModuleID;
0045:        import javax.enterprise.deploy.spi.status.ProgressObject;
0046:        import javax.management.MalformedObjectNameException;
0047:        import javax.management.ObjectName;
0048:        import javax.portlet.ActionRequest;
0049:        import javax.portlet.ActionResponse;
0050:        import javax.portlet.PortletConfig;
0051:        import javax.portlet.PortletException;
0052:        import javax.portlet.PortletRequest;
0053:        import javax.portlet.PortletRequestDispatcher;
0054:        import javax.portlet.PortletSession;
0055:        import javax.portlet.RenderRequest;
0056:        import javax.portlet.RenderResponse;
0057:        import javax.portlet.WindowState;
0058:        import javax.security.auth.Subject;
0059:        import javax.security.auth.spi.LoginModule;
0060:        import javax.xml.namespace.QName;
0061:
0062:        import org.apache.commons.logging.Log;
0063:        import org.apache.commons.logging.LogFactory;
0064:        import org.apache.geronimo.console.BasePortlet;
0065:        import org.apache.geronimo.console.car.ManagementHelper;
0066:        import org.apache.geronimo.console.util.PortletManager;
0067:        import org.apache.geronimo.deployment.xbeans.AbstractServiceType;
0068:        import org.apache.geronimo.deployment.xbeans.ArtifactType;
0069:        import org.apache.geronimo.deployment.xbeans.AttributeType;
0070:        import org.apache.geronimo.deployment.xbeans.DependenciesType;
0071:        import org.apache.geronimo.deployment.xbeans.EnvironmentType;
0072:        import org.apache.geronimo.deployment.xbeans.GbeanType;
0073:        import org.apache.geronimo.deployment.xbeans.ModuleDocument;
0074:        import org.apache.geronimo.deployment.xbeans.ModuleType;
0075:        import org.apache.geronimo.deployment.xbeans.ReferenceType;
0076:        import org.apache.geronimo.deployment.xbeans.ServiceDocument;
0077:        import org.apache.geronimo.deployment.xbeans.XmlAttributeType;
0078:        import org.apache.geronimo.gbean.AbstractName;
0079:        import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
0080:        import org.apache.geronimo.kernel.Kernel;
0081:        import org.apache.geronimo.kernel.KernelRegistry;
0082:        import org.apache.geronimo.kernel.config.Configuration;
0083:        import org.apache.geronimo.kernel.config.ConfigurationManager;
0084:        import org.apache.geronimo.kernel.config.ConfigurationModuleType;
0085:        import org.apache.geronimo.kernel.config.ConfigurationUtil;
0086:        import org.apache.geronimo.kernel.repository.Artifact;
0087:        import org.apache.geronimo.kernel.repository.ListableRepository;
0088:        import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
0089:        import org.apache.geronimo.security.jaas.JaasLoginModuleChain;
0090:        import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
0091:        import org.apache.geronimo.security.jaas.LoginModuleSettings;
0092:        import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
0093:        import org.apache.geronimo.security.jaas.LoginModuleControlFlagEditor;
0094:        import org.apache.geronimo.security.realm.SecurityRealm;
0095:        import org.apache.geronimo.security.realm.providers.FileAuditLoginModule;
0096:        import org.apache.geronimo.security.realm.providers.GeronimoPasswordCredentialLoginModule;
0097:        import org.apache.geronimo.security.realm.providers.NamedUsernamePasswordCredentialLoginModule;
0098:        import org.apache.geronimo.security.realm.providers.RepeatedFailureLockoutLoginModule;
0099:        import org.apache.geronimo.xbeans.geronimo.loginconfig.GerControlFlagType;
0100:        import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginConfigDocument;
0101:        import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginConfigType;
0102:        import org.apache.geronimo.xbeans.geronimo.loginconfig.GerLoginModuleType;
0103:        import org.apache.geronimo.xbeans.geronimo.loginconfig.GerOptionType;
0104:        import org.apache.xmlbeans.XmlCursor;
0105:        import org.apache.xmlbeans.XmlObject;
0106:        import org.apache.xmlbeans.XmlOptions;
0107:
0108:        /**
0109:         * A portlet that lists, creates, and edits security realms.
0110:         *
0111:         * @version $Rev: 614367 $ $Date: 2008-01-22 14:59:41 -0800 (Tue, 22 Jan 2008) $
0112:         */
0113:        public class SecurityRealmPortlet extends BasePortlet {
0114:            private final static Log log = LogFactory
0115:                    .getLog(SecurityRealmPortlet.class);
0116:            private final static String[] SKIP_ENTRIES_WITH = new String[] {
0117:                    "geronimo", "tomcat", "tranql", "commons", "directory",
0118:                    "activemq" };
0119:            private static final String LIST_VIEW = "/WEB-INF/view/realmwizard/list.jsp";
0120:            private static final String EDIT_VIEW = "/WEB-INF/view/realmwizard/edit.jsp";
0121:            private static final String SELECT_TYPE_VIEW = "/WEB-INF/view/realmwizard/selectType.jsp";
0122:            private static final String CONFIGURE_VIEW = "/WEB-INF/view/realmwizard/configure.jsp";
0123:            private static final String ADVANCED_VIEW = "/WEB-INF/view/realmwizard/advanced.jsp";
0124:            private static final String TEST_LOGIN_VIEW = "/WEB-INF/view/realmwizard/testLogin.jsp";
0125:            private static final String TEST_RESULTS_VIEW = "/WEB-INF/view/realmwizard/testResults.jsp";
0126:            private static final String SHOW_PLAN_VIEW = "/WEB-INF/view/realmwizard/showPlan.jsp";
0127:            private static final String USAGE_VIEW = "/WEB-INF/view/realmwizard/usage.jsp";
0128:            private static final String LIST_MODE = "list";
0129:            private static final String EDIT_MODE = "edit";
0130:            private static final String SELECT_TYPE_MODE = "type";
0131:            private static final String CONFIGURE_MODE = "configure";
0132:            private static final String ADVANCED_MODE = "advanced";
0133:            private static final String TEST_LOGIN_MODE = "test";
0134:            private static final String TEST_RESULTS_MODE = "results";
0135:            private static final String SHOW_PLAN_MODE = "plan";
0136:            private static final String EDIT_EXISTING_MODE = "editExisting";
0137:            private static final String USAGE_MODE = "usage";
0138:            private static final String SAVE_MODE = "save";
0139:            private static final String MODE_KEY = "mode";
0140:            private static final String CUSTOM_MODE = "custom";
0141:
0142:            private static Kernel kernel;
0143:
0144:            private PortletRequestDispatcher listView;
0145:            private PortletRequestDispatcher editView;
0146:            private PortletRequestDispatcher selectTypeView;
0147:            private PortletRequestDispatcher configureView;
0148:            private PortletRequestDispatcher advancedView;
0149:            private PortletRequestDispatcher testLoginView;
0150:            private PortletRequestDispatcher testResultsView;
0151:            private PortletRequestDispatcher planView;
0152:            private PortletRequestDispatcher usageView;
0153:            private static final QName GBEAN_QNAME = new QName(
0154:                    ServiceDocument.type.getDocumentElementName()
0155:                            .getNamespaceURI(), "gbean");
0156:
0157:            public void init(PortletConfig portletConfig)
0158:                    throws PortletException {
0159:                super .init(portletConfig);
0160:                kernel = KernelRegistry.getSingleKernel();
0161:                listView = portletConfig.getPortletContext()
0162:                        .getRequestDispatcher(LIST_VIEW);
0163:                editView = portletConfig.getPortletContext()
0164:                        .getRequestDispatcher(EDIT_VIEW);
0165:                selectTypeView = portletConfig.getPortletContext()
0166:                        .getRequestDispatcher(SELECT_TYPE_VIEW);
0167:                configureView = portletConfig.getPortletContext()
0168:                        .getRequestDispatcher(CONFIGURE_VIEW);
0169:                advancedView = portletConfig.getPortletContext()
0170:                        .getRequestDispatcher(ADVANCED_VIEW);
0171:                testLoginView = portletConfig.getPortletContext()
0172:                        .getRequestDispatcher(TEST_LOGIN_VIEW);
0173:                testResultsView = portletConfig.getPortletContext()
0174:                        .getRequestDispatcher(TEST_RESULTS_VIEW);
0175:                planView = portletConfig.getPortletContext()
0176:                        .getRequestDispatcher(SHOW_PLAN_VIEW);
0177:                usageView = portletConfig.getPortletContext()
0178:                        .getRequestDispatcher(USAGE_VIEW);
0179:            }
0180:
0181:            public void destroy() {
0182:                listView = null;
0183:                editView = null;
0184:                selectTypeView = null;
0185:                configureView = null;
0186:                advancedView = null;
0187:                testLoginView = null;
0188:                usageView = null;
0189:                planView = null;
0190:                super .destroy();
0191:            }
0192:
0193:            public void processAction(ActionRequest actionRequest,
0194:                    ActionResponse actionResponse) throws PortletException,
0195:                    IOException {
0196:                String mode = actionRequest.getParameter(MODE_KEY);
0197:                RealmData data = new RealmData();
0198:                data.load(actionRequest);
0199:                if (mode.equals(SELECT_TYPE_MODE)) {
0200:                    data.realmType = "Properties File Realm";
0201:                    actionResponse.setRenderParameter(MODE_KEY,
0202:                            SELECT_TYPE_MODE);
0203:                } else if (mode.equals("process-" + SELECT_TYPE_MODE)) {
0204:                    if (data.getName() != null
0205:                            && !data.getName().trim().equals("")) {
0206:                        // Config properties have to be set in render since they have values of null
0207:                        if (data.getRealmType().equals("Other")) {
0208:                            actionResponse.setRenderParameter(MODE_KEY,
0209:                                    CUSTOM_MODE);
0210:                        } else {
0211:                            actionResponse.setRenderParameter(MODE_KEY,
0212:                                    CONFIGURE_MODE);
0213:                        }
0214:                    } else {
0215:                        actionResponse.setRenderParameter(MODE_KEY,
0216:                                SELECT_TYPE_MODE);
0217:                    }
0218:                } else if (mode.equals("process-" + CONFIGURE_MODE)) {
0219:                    final String error = actionTestLoginModuleLoad(
0220:                            actionRequest, data);
0221:                    if (error == null) {
0222:                        actionResponse.setRenderParameter(MODE_KEY,
0223:                                ADVANCED_MODE);
0224:                    } else {
0225:                        actionResponse.setRenderParameter("LoginModuleError",
0226:                                error);
0227:                        actionResponse.setRenderParameter(MODE_KEY,
0228:                                CONFIGURE_MODE);
0229:                    }
0230:                } else if (mode.equals("process-" + ADVANCED_MODE)) {
0231:                    String test = actionRequest.getParameter("test");
0232:                    if (test == null || test.equals("true")) {
0233:                        actionResponse.setRenderParameter(MODE_KEY,
0234:                                TEST_LOGIN_MODE);
0235:                    } else {
0236:                        actionSaveRealm(actionRequest, data);
0237:                        actionResponse.setRenderParameter(MODE_KEY, LIST_MODE);
0238:                    }
0239:                } else if (mode.equals("process-" + TEST_LOGIN_MODE)) {
0240:                    actionAttemptLogin(data, actionRequest, actionRequest
0241:                            .getPortletSession(true), actionRequest
0242:                            .getParameter("username"), actionRequest
0243:                            .getParameter("password"));
0244:                    actionResponse.setRenderParameter(MODE_KEY,
0245:                            TEST_RESULTS_MODE);
0246:                } else if (mode.equals(SHOW_PLAN_MODE)) {
0247:                    XmlObject object = actionGeneratePlan(actionRequest, data);
0248:                    savePlanToSession(actionRequest.getPortletSession(true),
0249:                            object);
0250:                    actionResponse.setRenderParameter(MODE_KEY, SHOW_PLAN_MODE);
0251:                } else if (mode.equals(EDIT_EXISTING_MODE)) {
0252:                    actionLoadExistingRealm(actionRequest, data);
0253:                    actionResponse.setRenderParameter(MODE_KEY, EDIT_MODE);
0254:                } else if (mode.equals(CONFIGURE_MODE)) {
0255:                    if (data.getAbstractName() != null) {
0256:                        actionResponse.setRenderParameter(MODE_KEY, EDIT_MODE);
0257:                    } else if ((data.getRealmType() != null && data
0258:                            .getRealmType().equals("Other"))) {
0259:                        actionResponse
0260:                                .setRenderParameter(MODE_KEY, CUSTOM_MODE);
0261:                    } else {
0262:                        actionResponse.setRenderParameter(MODE_KEY,
0263:                                CONFIGURE_MODE);
0264:                    }
0265:                } else if (mode.equals(SAVE_MODE)) {
0266:                    actionSaveRealm(actionRequest, data);
0267:                    actionResponse.setRenderParameter(MODE_KEY, LIST_MODE);
0268:                } else {
0269:                    actionResponse.setRenderParameter(MODE_KEY, mode);
0270:                }
0271:                data.store(actionResponse);
0272:            }
0273:
0274:            protected void doView(RenderRequest renderRequest,
0275:                    RenderResponse renderResponse) throws IOException,
0276:                    PortletException {
0277:                if (WindowState.MINIMIZED
0278:                        .equals(renderRequest.getWindowState())) {
0279:                    return;
0280:                }
0281:                try {
0282:                    String mode = renderRequest.getParameter(MODE_KEY);
0283:                    RealmData data = new RealmData();
0284:                    data.load(renderRequest);
0285:                    renderRequest.setAttribute("realm", data);
0286:                    if (mode == null || mode.equals("")) {
0287:                        mode = LIST_MODE;
0288:                    }
0289:                    if (mode.equals(LIST_MODE)) {
0290:                        renderList(renderRequest, renderResponse);
0291:                    } else if (mode.equals(EDIT_MODE)
0292:                            || mode.equals(CUSTOM_MODE)) {
0293:                        renderRequest.setAttribute("mode", mode);
0294:                        if (mode.equals(CUSTOM_MODE))
0295:                            loadDriverJARList(renderRequest);
0296:                        renderEdit(renderRequest, renderResponse, data);
0297:                    } else if (mode.equals(SELECT_TYPE_MODE)) {
0298:                        renderSelectType(renderRequest, renderResponse);
0299:                    } else if (mode.equals(CONFIGURE_MODE)) {
0300:                        renderConfigure(renderRequest, renderResponse, data);
0301:                    } else if (mode.equals(ADVANCED_MODE)) {
0302:                        renderAdvanced(renderRequest, renderResponse, data);
0303:                    } else if (mode.equals(TEST_LOGIN_MODE)) {
0304:                        renderTestLoginForm(renderRequest, renderResponse);
0305:                    } else if (mode.equals(TEST_RESULTS_MODE)) {
0306:                        renderTestResults(renderRequest, renderResponse);
0307:                    } else if (mode.equals(SHOW_PLAN_MODE)) {
0308:                        renderPlan(renderRequest, renderResponse);
0309:                    } else if (mode.equals(USAGE_MODE)) {
0310:                        renderUsage(renderRequest, renderResponse);
0311:                    }
0312:                } catch (Throwable e) {
0313:                    log.error("Unable to render portlet", e);
0314:                }
0315:            }
0316:
0317:            private String actionTestLoginModuleLoad(PortletRequest request,
0318:                    RealmData data) {
0319:                Map options = new HashMap();
0320:                try {
0321:                    LoginModule module = loadModule(request, data, options);
0322:                    log.warn("Testing with options " + options);
0323:                    try {
0324:                        PortletManager
0325:                                .testLoginModule(request, module, options);
0326:                        return null;
0327:                    } catch (Exception e) {
0328:                        log.warn("Unable to initialize LoginModule", e);
0329:                        return "Unable to initialize LoginModule: "
0330:                                + e.getMessage();
0331:                    }
0332:                } catch (Exception e) {
0333:                    log.warn("Unable to load LoginModule class", e);
0334:                    return "Unable to load LoginModule class: "
0335:                            + e.getMessage();
0336:                }
0337:            }
0338:
0339:            private LoginModule loadModule(PortletRequest request,
0340:                    RealmData data, Map options) throws ClassNotFoundException,
0341:                    InstantiationException, IllegalAccessException {
0342:                ClassLoader loader = getClass().getClassLoader();
0343:                if (data.jar != null && !data.jar.equals("")) {
0344:                    try {
0345:                        Artifact one = Artifact.create(data.getJar());
0346:                        ListableRepository[] repos = PortletManager
0347:                                .getCurrentServer(request).getRepositories();
0348:                        for (int i = 0; i < repos.length; i++) {
0349:                            ListableRepository repo = repos[i];
0350:                            File file = repo.getLocation(one);
0351:                            if (file != null) {
0352:                                loader = new URLClassLoader(new URL[] { file
0353:                                        .toURL() }, loader);
0354:                                break;
0355:                            }
0356:                        }
0357:                    } catch (MalformedURLException e) {
0358:                        log.warn("Repository unable to look up JAR file", e);
0359:                    }
0360:                }
0361:                Class cls = loader.loadClass(getSelectedModule(data)
0362:                        .getClassName());
0363:                LoginModule module = (LoginModule) cls.newInstance();
0364:                for (Iterator it = data.getOptions().keySet().iterator(); it
0365:                        .hasNext();) {
0366:                    String key = (String) it.next();
0367:                    final Object value = data.getOptions().get(key);
0368:                    if (value != null && !value.equals("")) {
0369:                        options.put(key, value);
0370:                    }
0371:                }
0372:                options.put(JaasLoginModuleUse.CLASSLOADER_LM_OPTION, loader);
0373:                return module;
0374:            }
0375:
0376:            private void actionAttemptLogin(RealmData data,
0377:                    PortletRequest request, PortletSession session,
0378:                    String username, String password) {
0379:                session.removeAttribute("TestLoginPrincipals");
0380:                session.removeAttribute("TestLoginError");
0381:                Map options = new HashMap();
0382:                try {
0383:                    LoginModule module = loadModule(request, data, options);
0384:                    Subject sub = PortletManager.testLoginModule(request,
0385:                            module, options, username, password);
0386:                    session.setAttribute("TestLoginPrincipals", sub
0387:                            .getPrincipals());
0388:                } catch (Exception e) {
0389:                    log.warn("Test login failed", e);
0390:                    session.setAttribute("TestLoginError", "Login Failed: "
0391:                            + (e.getMessage() == null ? "no message" : e
0392:                                    .getMessage()));
0393:                }
0394:            }
0395:
0396:            private XmlObject actionGeneratePlan(PortletRequest request,
0397:                    RealmData data) {
0398:                normalize(data);
0399:                ModuleDocument doc = ModuleDocument.Factory.newInstance();
0400:                ModuleType root = doc.addNewModule();
0401:                EnvironmentType environment = root.addNewEnvironment();
0402:                ArtifactType configId = environment.addNewModuleId();
0403:                configId.setGroupId("console.realm");
0404:                String artifactId = data.getName();
0405:                if (artifactId.indexOf('/') != -1) {
0406:                    // slash in artifact-id results in invalid configuration-id and leads to deployment errors.
0407:                    // Note: 0x002F = '/'
0408:                    artifactId = artifactId.replaceAll("/", "%2F");
0409:                }
0410:                configId.setArtifactId(artifactId);
0411:                configId.setVersion("1.0");
0412:                configId.setType("car");
0413:
0414:                // Parent
0415:
0416:                DependenciesType dependenciesType = environment
0417:                        .addNewDependencies();
0418:                ArtifactType parent = dependenciesType.addNewDependency();
0419:                parent.setGroupId("org.apache.geronimo.framework");
0420:                parent.setArtifactId("j2ee-security");
0421:                parent.setType("car");
0422:                // Dependencies
0423:                if (data.getJar() != null) {
0424:                    ArtifactType artifactType = dependenciesType
0425:                            .addNewDependency();
0426:                    Artifact artifact = Artifact.create(data.getJar());
0427:                    artifactType.setGroupId(artifact.getGroupId());
0428:                    artifactType.setArtifactId(artifact.getArtifactId());
0429:                    artifactType.setVersion(artifact.getVersion().toString());
0430:                    artifactType.setType(artifact.getType());
0431:                }
0432:                // Build the realm GBean
0433:                GbeanType realm = GbeanType.Factory.newInstance();
0434:                realm.setName(data.getName());
0435:                realm
0436:                        .setClass1("org.apache.geronimo.security.realm.GenericSecurityRealm");
0437:                AttributeType realmName = realm.addNewAttribute();
0438:                realmName.setName("realmName");
0439:                realmName.setStringValue(data.getName());
0440:                ReferenceType serverInfo = realm.addNewReference();
0441:                serverInfo.setName2("ServerInfo");
0442:                serverInfo.setName((String) PortletManager.getNameFor(
0443:                        request,
0444:                        PortletManager.getCurrentServer(request)
0445:                                .getServerInfo()).getName().get("name"));
0446:                XmlAttributeType config = realm.addNewXmlReference();
0447:                // Construct the content to put in the XmlAttributeType
0448:                GerLoginConfigDocument lcDoc = GerLoginConfigDocument.Factory
0449:                        .newInstance();
0450:                GerLoginConfigType login = lcDoc.addNewLoginConfig();
0451:                for (int i = 0; i < data.getModules().length; i++) {
0452:                    LoginModuleDetails details = data.getModules()[i];
0453:                    if (details.getLoginDomainName() == null
0454:                            || details.getLoginDomainName().equals("")) {
0455:                        continue;
0456:                    }
0457:                    GerLoginModuleType module = login.addNewLoginModule();
0458:                    module
0459:                            .setControlFlag(details.getControlFlag().equals(
0460:                                    LoginModuleControlFlag.OPTIONAL) ? GerControlFlagType.OPTIONAL
0461:                                    : details.getControlFlag().equals(
0462:                                            LoginModuleControlFlag.REQUIRED) ? GerControlFlagType.REQUIRED
0463:                                            : details
0464:                                                    .getControlFlag()
0465:                                                    .equals(
0466:                                                            LoginModuleControlFlag.REQUISITE) ? GerControlFlagType.REQUISITE
0467:                                                    : details
0468:                                                            .getControlFlag()
0469:                                                            .equals(
0470:                                                                    LoginModuleControlFlag.SUFFICIENT) ? GerControlFlagType.SUFFICIENT
0471:                                                            : GerControlFlagType.OPTIONAL);
0472:                    module.setLoginDomainName(details.getLoginDomainName());
0473:                    module.setLoginModuleClass(details.getClassName());
0474:                    module.setWrapPrincipals(details.isWrapPrincipals());
0475:                    for (Iterator it = details.getOptions().entrySet()
0476:                            .iterator(); it.hasNext();) {
0477:                        Map.Entry entry = (Map.Entry) it.next();
0478:                        GerOptionType option = module.addNewOption();
0479:                        option.setName((String) entry.getKey());
0480:                        option.setStringValue((String) entry.getValue());
0481:                    }
0482:
0483:                    // bit of a hack -- to put the DataSource module in as a parent for SQL modules
0484:                    if (details.getClassName().indexOf("SQL") > -1) {
0485:                        String poolName = (String) details.getOptions().get(
0486:                                "dataSourceName");
0487:                        String appName = (String) details.getOptions().get(
0488:                                "dataSourceApplication");
0489:                        if (poolName != null) {
0490:                            if (appName == null)
0491:                                appName = "null";
0492:                            JCAManagedConnectionFactory[] factories = PortletManager
0493:                                    .getOutboundFactoriesOfType(request,
0494:                                            "javax.sql.DataSource");
0495:                            for (int j = 0; j < factories.length; j++) {
0496:                                JCAManagedConnectionFactory factory = factories[j];
0497:                                try {
0498:                                    ObjectName objectName = ObjectName
0499:                                            .getInstance(factory
0500:                                                    .getObjectName());
0501:                                    final String testName = objectName
0502:                                            .getKeyProperty(NameFactory.J2EE_NAME);
0503:                                    final String testApp = objectName
0504:                                            .getKeyProperty(NameFactory.J2EE_APPLICATION);
0505:                                    if (testName.equals(poolName)
0506:                                            && testApp.equals(appName)) {
0507:                                        String moduleName = objectName
0508:                                                .getKeyProperty(NameFactory.JCA_RESOURCE);
0509:
0510:                                        ArtifactType artifactType = dependenciesType
0511:                                                .addNewDependency();
0512:                                        Artifact artifact = Artifact
0513:                                                .create(moduleName);
0514:                                        artifactType.setGroupId(artifact
0515:                                                .getGroupId());
0516:                                        artifactType.setArtifactId(artifact
0517:                                                .getArtifactId());
0518:                                        artifactType.setVersion(artifact
0519:                                                .getVersion().toString());
0520:                                        artifactType
0521:                                                .setType(artifact.getType());
0522:                                        break;
0523:                                    }
0524:                                } catch (MalformedObjectNameException e) {
0525:                                    log.error("Unable to parse ObjectName", e);
0526:                                }
0527:                            }
0528:                        }
0529:                    }
0530:                }
0531:                // Copy the content into the XmlAttributeType
0532:                XmlCursor loginCursor = lcDoc.newCursor();
0533:                loginCursor.toFirstContentToken();
0534:                XmlCursor destination = config.newCursor();
0535:                destination.toNextToken();
0536:                loginCursor.moveXml(destination);
0537:                loginCursor.dispose();
0538:                destination.dispose();
0539:                config.setName("LoginModuleConfiguration");
0540:                root.setServiceArray(new AbstractServiceType[] { realm });
0541:
0542:                //Above code inserts gbean using xsi:type=dep:GBeanType.  We also need to account for the substitution group
0543:                //by changing the qname:
0544:                XmlCursor gbeanCursor = root.newCursor();
0545:                try {
0546:                    if (!gbeanCursor.toChild(ServiceDocument.type
0547:                            .getDocumentElementName())) {
0548:                        throw new RuntimeException(
0549:                                "Could not find service element");
0550:                    }
0551:                    gbeanCursor.setName(GBEAN_QNAME);
0552:                } finally {
0553:                    gbeanCursor.dispose();
0554:                }
0555:
0556:                return doc;
0557:            }
0558:
0559:            private void actionLoadExistingRealm(PortletRequest request,
0560:                    RealmData data) {
0561:                SecurityRealm realm = (SecurityRealm) PortletManager
0562:                        .getManagedBean(request, new AbstractName(URI
0563:                                .create(data.getAbstractName())));
0564:                data.name = realm.getRealmName();
0565:                List list = new ArrayList();
0566:                JaasLoginModuleChain node = realm.getLoginModuleChain();
0567:                while (node != null) {
0568:                    LoginModuleDetails details = new LoginModuleDetails();
0569:                    details.setControlFlag(node.getControlFlag());
0570:                    LoginModuleSettings module = node.getLoginModule();
0571:                    details.setLoginDomainName(module.getLoginDomainName());
0572:                    details.setClassName(module.getLoginModuleClass());
0573:                    details.setWrapPrincipals(module.isWrapPrincipals());
0574:                    details.setOptions(module.getOptions());
0575:                    list.add(details);
0576:                    node = node.getNext();
0577:                    if (node == null) {
0578:                        break;
0579:                    }
0580:                }
0581:                data.modules = (LoginModuleDetails[]) list
0582:                        .toArray(new LoginModuleDetails[list.size()]);
0583:            }
0584:
0585:            private void actionSaveRealm(PortletRequest request, RealmData data) {
0586:                normalize(data);
0587:                if (data.getAbstractName() == null
0588:                        || data.getAbstractName().equals("")) { // we're creating a new realm
0589:                    try {
0590:                        XmlObject plan = actionGeneratePlan(request, data);
0591:                        data.name = data.name.replaceAll("\\s", "");
0592:                        DeploymentManager mgr = ManagementHelper
0593:                                .getManagementHelper(request)
0594:                                .getDeploymentManager();
0595:                        File tempFile = File.createTempFile(
0596:                                "console-deployment", ".xml");
0597:                        tempFile.deleteOnExit();
0598:                        log.debug("Writing security realm deployment plan to "
0599:                                + tempFile.getAbsolutePath());
0600:                        PrintWriter out = new PrintWriter(new FileWriter(
0601:                                tempFile));
0602:                        savePlanToStream(plan, out);
0603:                        out.flush();
0604:                        out.close();
0605:                        Target[] targets = mgr.getTargets();
0606:                        if (null == targets) {
0607:                            throw new IllegalStateException(
0608:                                    "No target to distribute to");
0609:                        }
0610:                        targets = new Target[] { targets[0] };
0611:
0612:                        ProgressObject po = mgr.distribute(targets, null,
0613:                                tempFile);
0614:                        waitForProgress(po);
0615:                        if (po.getDeploymentStatus().isCompleted()) {
0616:                            TargetModuleID[] ids = po
0617:                                    .getResultTargetModuleIDs();
0618:                            po = mgr.start(ids);
0619:                            waitForProgress(po);
0620:                            if (po.getDeploymentStatus().isCompleted()) {
0621:                                log.info("Deployment completed successfully!");
0622:                            }
0623:                        }
0624:                    } catch (IOException e) {
0625:                        log.error("Unable to save security realm", e);
0626:                    }
0627:                } else {
0628:                    SecurityRealm realm = (SecurityRealm) PortletManager
0629:                            .getManagedBean(request, new AbstractName(URI
0630:                                    .create(data.getAbstractName())));
0631:                    // index existing modules
0632:                    Map nodes = new HashMap();
0633:                    JaasLoginModuleChain node = realm.getLoginModuleChain();
0634:                    while (node != null) {
0635:                        LoginModuleSettings module = node.getLoginModule();
0636:                        nodes.put(module.getLoginDomainName(), node);
0637:                        node = node.getNext();
0638:                        if (node == null) {
0639:                            break;
0640:                        }
0641:                    }
0642:                    // apply settings
0643:                    for (int i = 0; i < data.getModules().length; i++) {
0644:                        LoginModuleDetails details = data.getModules()[i];
0645:                        node = (JaasLoginModuleChain) nodes.get(details
0646:                                .getLoginDomainName());
0647:                        node.setControlFlag(details.getControlFlag());
0648:                        LoginModuleSettings module = node.getLoginModule();
0649:                        module.setOptions(details.getOptions());
0650:                        module.setWrapPrincipals(details.isWrapPrincipals());
0651:                        module.setLoginModuleClass(details.getClassName());
0652:                    }
0653:                }
0654:            }
0655:
0656:            private void renderList(RenderRequest request,
0657:                    RenderResponse response) throws IOException,
0658:                    PortletException {
0659:                // Unfortunately there are two classes named SecurityRealm; one extends the other
0660:                // The array type is management.geronimo.SecurityRealm (the superclass)
0661:                // The array entry types are security.realm.SecurityRealm (the subclass)
0662:                org.apache.geronimo.management.geronimo.SecurityRealm[] realms = PortletManager
0663:                        .getCurrentServer(request).getSecurityRealms();
0664:                ExistingRealm[] results = new ExistingRealm[realms.length];
0665:
0666:                // ConfigurationManager is used to determine if the SecurityRealm is deployed as a "SERVICE", i.e., "Server-wide"
0667:                ConfigurationManager configMgr = null;
0668:                if (results.length > 0) {
0669:                    // Needed only when there are any SecurityRealms
0670:                    configMgr = ConfigurationUtil
0671:                            .getConfigurationManager(kernel);
0672:                }
0673:                for (int i = 0; i < results.length; i++) {
0674:                    AbstractName abstractName = PortletManager.getNameFor(
0675:                            request, realms[i]);
0676:                    String parent;
0677:                    Configuration parentConfig = configMgr
0678:                            .getConfiguration(abstractName.getArtifact());
0679:                    ConfigurationModuleType parentType = parentConfig
0680:                            .getModuleType();
0681:                    if (ConfigurationModuleType.SERVICE.equals(parentType)) {
0682:                        parent = null; // Server-wide
0683:                    } else {
0684:                        parent = abstractName.getArtifact().toString();
0685:                    }
0686:                    results[i] = new ExistingRealm(realms[i].getRealmName(),
0687:                            abstractName, parent);
0688:                }
0689:                // Once done, release the ConfigurationManager
0690:                if (configMgr != null) {
0691:                    ConfigurationUtil.releaseConfigurationManager(kernel,
0692:                            configMgr);
0693:                }
0694:                request.setAttribute("realms", results);
0695:                listView.include(request, response);
0696:            }
0697:
0698:            private void renderEdit(RenderRequest request,
0699:                    RenderResponse response, RealmData data)
0700:                    throws IOException, PortletException {
0701:                normalize(data);
0702:                editView.include(request, response);
0703:            }
0704:
0705:            private void renderSelectType(RenderRequest request,
0706:                    RenderResponse response) throws IOException,
0707:                    PortletException {
0708:                request.setAttribute("moduleTypes", MasterLoginModuleInfo
0709:                        .getAllModules());
0710:                selectTypeView.include(request, response);
0711:            }
0712:
0713:            private void renderConfigure(RenderRequest request,
0714:                    RenderResponse response, RealmData data)
0715:                    throws IOException, PortletException {
0716:                // Pass errors through
0717:                if (request.getParameter("LoginModuleError") != null) {
0718:                    request.setAttribute("LoginModuleError", request
0719:                            .getParameter("LoginModuleError"));
0720:                }
0721:                // Clear out any cached modules
0722:                data.modules = null;
0723:                // Configure option list
0724:                MasterLoginModuleInfo info = getSelectedModule(data);
0725:                for (int i = 0; i < info.getOptions().length; i++) {
0726:                    MasterLoginModuleInfo.OptionInfo option = info.getOptions()[i];
0727:                    if (!data.getOptions().containsKey(option.getName())) {
0728:                        data.getOptions().put(option.getName(), null);
0729:                    }
0730:                }
0731:                data.reorderOptions(info.getOptions());
0732:                request.setAttribute("optionMap", info.getOptionMap());
0733:                if (info.getName().indexOf("SQL") > -1) {
0734:                    loadDriverJARList(request);
0735:                    loadDatabasePoolList(request);
0736:                }
0737:                configureView.include(request, response);
0738:            }
0739:
0740:            private void renderAdvanced(RenderRequest request,
0741:                    RenderResponse response, RealmData data)
0742:                    throws IOException, PortletException {
0743:                // Clear out any cached modules
0744:                data.modules = null;
0745:                // Show the page
0746:                advancedView.include(request, response);
0747:            }
0748:
0749:            private void renderTestLoginForm(RenderRequest request,
0750:                    RenderResponse response) throws IOException,
0751:                    PortletException {
0752:                testLoginView.include(request, response);
0753:            }
0754:
0755:            private void renderTestResults(RenderRequest request,
0756:                    RenderResponse response) throws IOException,
0757:                    PortletException {
0758:                PortletSession session = request.getPortletSession();
0759:                String status = (String) session.getAttribute("TestLoginError");
0760:                if (status == null) {
0761:                    Set principals = (Set) session
0762:                            .getAttribute("TestLoginPrincipals");
0763:                    status = "Login succeeded with "
0764:                            + (principals == null ? 0 : principals.size())
0765:                            + " principals";
0766:                    request.setAttribute("principals", principals);
0767:                }
0768:                request.setAttribute("LoginResults", status);
0769:                testResultsView.include(request, response);
0770:            }
0771:
0772:            private void renderPlan(RenderRequest request,
0773:                    RenderResponse response) throws IOException,
0774:                    PortletException {
0775:                String plan = (String) request.getPortletSession()
0776:                        .getAttribute("SecurityRealmPlan");
0777:                request.setAttribute("deploymentPlan", plan);
0778:                planView.include(request, response);
0779:            }
0780:
0781:            private void renderUsage(RenderRequest request,
0782:                    RenderResponse response) throws IOException,
0783:                    PortletException {
0784:                usageView.include(request, response);
0785:            }
0786:
0787:            private static MasterLoginModuleInfo getSelectedModule(
0788:                    RealmData data) {
0789:                MasterLoginModuleInfo[] all = MasterLoginModuleInfo
0790:                        .getAllModules();
0791:                for (int i = 0; i < all.length; i++) {
0792:                    MasterLoginModuleInfo info = all[i];
0793:                    if (info.getName().equals(data.getRealmType())) {
0794:                        return info;
0795:                    }
0796:                }
0797:                return null;
0798:            }
0799:
0800:            private void loadDatabasePoolList(RenderRequest renderRequest) {
0801:                JCAManagedConnectionFactory[] factories = PortletManager
0802:                        .getOutboundFactoriesOfType(renderRequest,
0803:                                "javax.sql.DataSource");
0804:                List pools = new ArrayList();
0805:                try {
0806:                    for (int i = 0; i < factories.length; i++) {
0807:                        JCAManagedConnectionFactory factory = factories[i];
0808:                        ObjectName objectName = ObjectName.getInstance(factory
0809:                                .getObjectName());
0810:                        final String name = objectName
0811:                                .getKeyProperty(NameFactory.J2EE_NAME);
0812:                        String display = name;
0813:                        final String appName = objectName
0814:                                .getKeyProperty(NameFactory.J2EE_APPLICATION);
0815:                        if (appName != null && !appName.equals("null")) {
0816:                            display = display + " (" + appName + ")";
0817:                        }
0818:                        pools.add(new DatabasePool(name, display, appName,
0819:                                PortletManager.getNameFor(renderRequest,
0820:                                        factory)));
0821:                    }
0822:                    renderRequest.setAttribute("pools", pools);
0823:                } catch (MalformedObjectNameException e) {
0824:                    log.error("Unable to parse ObjectName", e);
0825:                }
0826:            }
0827:
0828:            private void loadDriverJARList(RenderRequest renderRequest) {
0829:                // List the available JARs
0830:                List list = new ArrayList();
0831:                ListableRepository[] repos = PortletManager.getCurrentServer(
0832:                        renderRequest).getRepositories();
0833:                for (int i = 0; i < repos.length; i++) {
0834:                    ListableRepository repo = repos[i];
0835:
0836:                    SortedSet artifacts = repo.list();
0837:                    outer: for (Iterator iterator = artifacts.iterator(); iterator
0838:                            .hasNext();) {
0839:                        Artifact artifact = (Artifact) iterator.next();
0840:                        String test = artifact.toString();
0841:                        // todo should only test groupId and should check for long (org.apache.geronimo) and short form
0842:                        for (int k = 0; k < SKIP_ENTRIES_WITH.length; k++) {
0843:                            String skip = SKIP_ENTRIES_WITH[k];
0844:                            if (test.indexOf(skip) > -1) {
0845:                                continue outer;
0846:                            }
0847:                        }
0848:                        list.add(test);
0849:                    }
0850:                }
0851:                Collections.sort(list);
0852:                renderRequest.setAttribute("jars", list);
0853:            }
0854:
0855:            private void savePlanToSession(PortletSession session,
0856:                    XmlObject object) {
0857:                StringWriter out = new StringWriter();
0858:                try {
0859:                    savePlanToStream(object, out);
0860:                    session.setAttribute("SecurityRealmPlan", out.getBuffer()
0861:                            .toString());
0862:                } catch (IOException e) {
0863:                    log.error("Unable to write deployment plan", e);
0864:                }
0865:            }
0866:
0867:            private void savePlanToStream(XmlObject object, Writer out)
0868:                    throws IOException {
0869:                XmlOptions options = new XmlOptions();
0870:                options.setSavePrettyPrint();
0871:                options.setSavePrettyPrintIndent(4);
0872:                options.setUseDefaultNamespace();
0873:                object.save(out, options);
0874:                out.close();
0875:            }
0876:
0877:            private static void waitForProgress(ProgressObject po) {
0878:                while (po.getDeploymentStatus().isRunning()) {
0879:                    try {
0880:                        Thread.sleep(100);
0881:                    } catch (InterruptedException e) {
0882:                        log.error(e.getMessage(), e);
0883:                    }
0884:                }
0885:            }
0886:
0887:            public static void normalize(RealmData data) {
0888:                List list = new ArrayList();
0889:                if (data.modules == null) {
0890:                    LoginModuleDetails module = new LoginModuleDetails();
0891:                    module.setClassName(getSelectedModule(data).getClassName());
0892:                    module.setControlFlag(LoginModuleControlFlag.REQUIRED);
0893:                    module.setLoginDomainName(data.getName());
0894:                    Map<String, Object> props = module.getOptions();
0895:                    for (Iterator it = data.getOptions().entrySet().iterator(); it
0896:                            .hasNext();) {
0897:                        Map.Entry entry = (Map.Entry) it.next();
0898:                        props.put((String) entry.getKey(), (String) entry
0899:                                .getValue());
0900:                    }
0901:                    list.add(module);
0902:                    if (data.isStorePassword()) {
0903:                        module = new LoginModuleDetails();
0904:                        module
0905:                                .setClassName(GeronimoPasswordCredentialLoginModule.class
0906:                                        .getName());
0907:                        module.setControlFlag(LoginModuleControlFlag.OPTIONAL);
0908:                        module.setLoginDomainName(data.getName() + "-Password");
0909:                        list.add(module);
0910:                    }
0911:                    if (data.getAuditPath() != null) {
0912:                        module = new LoginModuleDetails();
0913:                        module.setClassName(FileAuditLoginModule.class
0914:                                .getName());
0915:                        module.setControlFlag(LoginModuleControlFlag.OPTIONAL);
0916:                        module.setLoginDomainName(data.getName() + "-Audit");
0917:                        props = module.getOptions();
0918:                        props.put("file", data.getAuditPath());
0919:                        list.add(module);
0920:                    }
0921:                    if (data.isLockoutEnabled()) {
0922:                        module = new LoginModuleDetails();
0923:                        module
0924:                                .setClassName(RepeatedFailureLockoutLoginModule.class
0925:                                        .getName());
0926:                        module.setControlFlag(LoginModuleControlFlag.REQUISITE);
0927:                        module.setLoginDomainName(data.getName() + "-Lockout");
0928:                        props = module.getOptions();
0929:                        props.put("failureCount", data.getLockoutCount());
0930:                        props.put("failurePeriodSecs", data.getLockoutWindow());
0931:                        props.put("lockoutDurationSecs", data
0932:                                .getLockoutDuration());
0933:                        list.add(module);
0934:                    }
0935:                    if (data.getCredentialName() != null) {
0936:                        module = new LoginModuleDetails();
0937:                        module
0938:                                .setClassName(NamedUsernamePasswordCredentialLoginModule.class
0939:                                        .getName());
0940:                        module.setControlFlag(LoginModuleControlFlag.OPTIONAL);
0941:                        module.setLoginDomainName(data.getName() + "-NamedUPC");
0942:                        props = module.getOptions();
0943:                        props
0944:                                .put(
0945:                                        NamedUsernamePasswordCredentialLoginModule.CREDENTIAL_NAME,
0946:                                        data.getCredentialName());
0947:                        list.add(module);
0948:                    }
0949:                } else {
0950:                    list.addAll(Arrays.asList(data.modules));
0951:                }
0952:                if (data.getAbstractName() == null) {
0953:                    for (int i = list.size(); i < 5; i++) {
0954:                        LoginModuleDetails module = new LoginModuleDetails();
0955:                        list.add(module);
0956:                    }
0957:                }
0958:                data.modules = (LoginModuleDetails[]) list
0959:                        .toArray(new LoginModuleDetails[list.size()]);
0960:            }
0961:
0962:            public static class RealmData implements  Serializable {
0963:                private String name;
0964:                private String realmType;
0965:                private String jar;
0966:                private Map options = new LinkedHashMap();
0967:                private String auditPath;
0968:                private String lockoutCount;
0969:                private String lockoutWindow;
0970:                private String lockoutDuration;
0971:                private boolean storePassword;
0972:                private String abstractName; // used when editing existing realms
0973:                private LoginModuleDetails[] modules;
0974:                private String credentialName;
0975:
0976:                public void load(PortletRequest request) {
0977:                    name = request.getParameter("name");
0978:                    if (name != null && name.equals(""))
0979:                        name = null;
0980:                    realmType = request.getParameter("realmType");
0981:                    if (realmType != null && realmType.equals(""))
0982:                        realmType = null;
0983:                    jar = request.getParameter("jar");
0984:                    if (jar != null && jar.equals(""))
0985:                        jar = null;
0986:                    auditPath = request.getParameter("auditPath");
0987:                    if (auditPath != null && auditPath.equals(""))
0988:                        auditPath = null;
0989:                    lockoutCount = request.getParameter("lockoutCount");
0990:                    if (lockoutCount != null && lockoutCount.equals(""))
0991:                        lockoutCount = null;
0992:                    lockoutWindow = request.getParameter("lockoutWindow");
0993:                    if (lockoutWindow != null && lockoutWindow.equals(""))
0994:                        lockoutWindow = null;
0995:                    lockoutDuration = request.getParameter("lockoutDuration");
0996:                    if (lockoutDuration != null && lockoutDuration.equals(""))
0997:                        lockoutDuration = null;
0998:                    abstractName = request.getParameter("abstractName");
0999:                    if (abstractName != null && abstractName.equals(""))
1000:                        abstractName = null;
1001:                    String test = request.getParameter("storePassword");
1002:                    storePassword = test != null && !test.equals("")
1003:                            && !test.equals("false");
1004:                    credentialName = request.getParameter("credentialName");
1005:                    if (credentialName != null && credentialName.equals(""))
1006:                        credentialName = null;
1007:                    Map map = request.getParameterMap();
1008:                    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
1009:                        String key = (String) it.next();
1010:                        if (key.startsWith("option-")) {
1011:                            if (key.equals("option-databasePoolAbstractName")) { // special handling for a data source, where there's one select corresponding to two properties
1012:                                String nameString = request.getParameter(key);
1013:                                if (nameString != null
1014:                                        && !nameString.equals("")) {
1015:                                    AbstractName an = new AbstractName(URI
1016:                                            .create(nameString));
1017:                                    options
1018:                                            .put(
1019:                                                    "dataSourceName",
1020:                                                    an
1021:                                                            .getNameProperty(NameFactory.J2EE_NAME));
1022:                                    options
1023:                                            .put(
1024:                                                    "dataSourceApplication",
1025:                                                    an
1026:                                                            .getNameProperty(NameFactory.J2EE_APPLICATION));
1027:                                }
1028:                            } else {
1029:                                final String optionName = key.substring(7);
1030:                                final String value = request.getParameter(key);
1031:                                if (value != null && !value.equals("")) {
1032:                                    options.put(optionName, value);
1033:                                }
1034:                            }
1035:                        }
1036:                    }
1037:                    int count = 0;
1038:                    List list = new ArrayList();
1039:                    while (true) {
1040:                        int index = count;
1041:                        ++count;
1042:                        String name = request.getParameter("module-domain-"
1043:                                + index);
1044:                        if (name == null || name.equals(""))
1045:                            break;
1046:                        LoginModuleDetails details = new LoginModuleDetails();
1047:                        details.setLoginDomainName(name);
1048:                        String cls = request.getParameter("module-class-"
1049:                                + index);
1050:                        if (cls == null || cls.equals(""))
1051:                            continue;
1052:                        details.setClassName(cls);
1053:                        String flag = request.getParameter("module-control-"
1054:                                + index);
1055:                        if (flag == null || flag.equals(""))
1056:                            continue;
1057:                        details.setControlFlag(toFlag(flag));
1058:                        String wrap = request.getParameter("module-wrap-"
1059:                                + index);
1060:                        if (wrap == null || wrap.equals(""))
1061:                            continue;
1062:                        details.setWrapPrincipals(Boolean.valueOf(wrap)
1063:                                .booleanValue());
1064:                        String options = request.getParameter("module-options-"
1065:                                + index);
1066:                        if (options != null && !options.equals("")) {
1067:                            BufferedReader in = new BufferedReader(
1068:                                    new StringReader(options));
1069:                            String line;
1070:                            try {
1071:                                while ((line = in.readLine()) != null) {
1072:                                    if (line.startsWith("#") || line.equals("")) {
1073:                                        continue;
1074:                                    }
1075:                                    int pos = line.indexOf('=');
1076:                                    if (pos > -1) {
1077:                                        details.getOptions().put(
1078:                                                line.substring(0, pos),
1079:                                                line.substring(pos + 1));
1080:                                    }
1081:                                }
1082:                            } catch (IOException e) {
1083:                                log.error("Unable to read properties '"
1084:                                        + options + "'", e);
1085:                            }
1086:                        }
1087:                        list.add(details);
1088:                    }
1089:                    if (list.size() > 0) {
1090:                        modules = (LoginModuleDetails[]) list
1091:                                .toArray(new LoginModuleDetails[list.size()]);
1092:                    }
1093:                }
1094:
1095:                private LoginModuleControlFlag toFlag(String flag) {
1096:                    LoginModuleControlFlagEditor editor = new LoginModuleControlFlagEditor();
1097:                    editor.setAsText(flag);
1098:                    return (LoginModuleControlFlag) editor.getValue();
1099:                }
1100:
1101:                public void reorderOptions(
1102:                        MasterLoginModuleInfo.OptionInfo[] info) {
1103:                    if (info == null || info.length == 0) {
1104:                        return; // Probably SQL or something that handles this manually
1105:                    }
1106:                    Map map = new LinkedHashMap();
1107:                    for (int i = 0; i < info.length; i++) {
1108:                        if (options.containsKey(info[i].getName())) {
1109:                            map.put(info[i].getName(), options.get(info[i]
1110:                                    .getName()));
1111:                        }
1112:                    }
1113:                    options = map;
1114:                }
1115:
1116:                public void store(ActionResponse response) {
1117:                    if (name != null)
1118:                        response.setRenderParameter("name", name);
1119:                    if (realmType != null)
1120:                        response.setRenderParameter("realmType", realmType);
1121:                    if (jar != null)
1122:                        response.setRenderParameter("jar", jar);
1123:                    if (auditPath != null)
1124:                        response.setRenderParameter("auditPath", auditPath);
1125:                    if (lockoutCount != null)
1126:                        response.setRenderParameter("lockoutCount",
1127:                                lockoutCount);
1128:                    if (lockoutWindow != null)
1129:                        response.setRenderParameter("lockoutWindow",
1130:                                lockoutWindow);
1131:                    if (lockoutDuration != null)
1132:                        response.setRenderParameter("lockoutDuration",
1133:                                lockoutDuration);
1134:                    if (abstractName != null)
1135:                        response.setRenderParameter("abstractName",
1136:                                abstractName);
1137:                    if (storePassword)
1138:                        response.setRenderParameter("storePassword", "true");
1139:                    if (credentialName != null)
1140:                        response.setRenderParameter("credentialName",
1141:                                credentialName);
1142:                    for (Iterator it = options.keySet().iterator(); it
1143:                            .hasNext();) {
1144:                        String name = (String) it.next();
1145:                        String value = (String) options.get(name);
1146:                        if (value != null) {
1147:                            response
1148:                                    .setRenderParameter("option-" + name, value);
1149:                        }
1150:                    }
1151:                    if (modules != null) {
1152:                        for (int i = 0; i < modules.length; i++) {
1153:                            LoginModuleDetails module = modules[i];
1154:                            if (module.getLoginDomainName() != null)
1155:                                response.setRenderParameter("module-domain-"
1156:                                        + i, module.getLoginDomainName());
1157:                            if (module.getClassName() != null)
1158:                                response.setRenderParameter(
1159:                                        "module-class-" + i, module
1160:                                                .getClassName());
1161:                            if (module.getControlFlag() != null)
1162:                                response
1163:                                        .setRenderParameter("module-control-"
1164:                                                + i, module.getControlFlag()
1165:                                                .toString());
1166:                            response
1167:                                    .setRenderParameter("module-wrap-" + i,
1168:                                            Boolean.toString(module
1169:                                                    .isWrapPrincipals()));
1170:                            if (module.getOptions().size() > 0)
1171:                                response.setRenderParameter("module-options-"
1172:                                        + i, module.getOptionString());
1173:                        }
1174:                    }
1175:                }
1176:
1177:                public String getName() {
1178:                    return name;
1179:                }
1180:
1181:                public String getRealmType() {
1182:                    return realmType;
1183:                }
1184:
1185:                public Map getOptions() {
1186:                    return options;
1187:                }
1188:
1189:                public Set getOptionNames() {
1190:                    return options.keySet();
1191:                }
1192:
1193:                public String getJar() {
1194:                    return jar;
1195:                }
1196:
1197:                public String getAuditPath() {
1198:                    return auditPath;
1199:                }
1200:
1201:                public String getLockoutCount() {
1202:                    return lockoutCount;
1203:                }
1204:
1205:                public String getLockoutWindow() {
1206:                    return lockoutWindow;
1207:                }
1208:
1209:                public String getLockoutDuration() {
1210:                    return lockoutDuration;
1211:                }
1212:
1213:                public boolean isStorePassword() {
1214:                    return storePassword;
1215:                }
1216:
1217:                public boolean isLockoutEnabled() {
1218:                    return lockoutCount != null || lockoutWindow != null
1219:                            || lockoutDuration != null;
1220:                }
1221:
1222:                public String getCredentialName() {
1223:                    return credentialName;
1224:                }
1225:
1226:                public String getAbstractName() {
1227:                    return abstractName;
1228:                }
1229:
1230:                public boolean isTestable() {
1231:                    return getSelectedModule(this ).isTestable();
1232:                }
1233:
1234:                public LoginModuleDetails[] getModules() {
1235:                    return modules;
1236:                }
1237:            }
1238:
1239:            public static class LoginModuleDetails implements  Serializable {
1240:                private String loginDomainName;
1241:                private String className;
1242:                private LoginModuleControlFlag controlFlag;
1243:                private boolean wrapPrincipals = false;
1244:                private Map<String, Object> options = new HashMap<String, Object>();
1245:
1246:                public String getLoginDomainName() {
1247:                    return loginDomainName;
1248:                }
1249:
1250:                public void setLoginDomainName(String loginDomainName) {
1251:                    this .loginDomainName = loginDomainName;
1252:                }
1253:
1254:                public String getClassName() {
1255:                    return className;
1256:                }
1257:
1258:                public void setClassName(String className) {
1259:                    this .className = className;
1260:                }
1261:
1262:                public LoginModuleControlFlag getControlFlag() {
1263:                    return controlFlag;
1264:                }
1265:
1266:                public void setControlFlag(LoginModuleControlFlag controlFlag) {
1267:                    this .controlFlag = controlFlag;
1268:                }
1269:
1270:                public Map<String, Object> getOptions() {
1271:                    return options;
1272:                }
1273:
1274:                public void setOptions(Map<String, Object> options) {
1275:                    this .options = options;
1276:                }
1277:
1278:                public boolean isWrapPrincipals() {
1279:                    return wrapPrincipals;
1280:                }
1281:
1282:                public void setWrapPrincipals(boolean wrapPrincipals) {
1283:                    this .wrapPrincipals = wrapPrincipals;
1284:                }
1285:
1286:                public String getOptionString() {
1287:                    StringBuffer buf = new StringBuffer();
1288:                    for (Iterator it = options.keySet().iterator(); it
1289:                            .hasNext();) {
1290:                        String key = (String) it.next();
1291:                        buf.append(key).append("=").append(options.get(key))
1292:                                .append("\n");
1293:                    }
1294:                    return buf.toString();
1295:                }
1296:            }
1297:
1298:            public static class ExistingRealm implements  Serializable {
1299:                private final String name;
1300:                private final String abstractName;
1301:                private final String parentName;
1302:
1303:                public ExistingRealm(String name, AbstractName abstractName,
1304:                        String parent) {
1305:                    this .name = name;
1306:                    this .abstractName = abstractName.toString();
1307:                    parentName = parent;
1308:                }
1309:
1310:                public String getName() {
1311:                    return name;
1312:                }
1313:
1314:                public String getAbstractName() {
1315:                    return abstractName;
1316:                }
1317:
1318:                public String getParentName() {
1319:                    return parentName;
1320:                }
1321:
1322:            }
1323:
1324:            public static class DatabasePool implements  Serializable,
1325:                    Comparable {
1326:                private final String name;
1327:                private final String displayName;
1328:                private final String applicationName;
1329:                private final String abstractName;
1330:
1331:                public DatabasePool(String name, String displayName,
1332:                        String applicationName, AbstractName abstractName) {
1333:                    this .name = name;
1334:                    this .displayName = displayName;
1335:                    this .applicationName = applicationName;
1336:                    this .abstractName = abstractName.toString();
1337:                }
1338:
1339:                public String getName() {
1340:                    return name;
1341:                }
1342:
1343:                public String getApplicationName() {
1344:                    return applicationName;
1345:                }
1346:
1347:                public String getAbstractName() {
1348:                    return abstractName;
1349:                }
1350:
1351:                public String getDisplayName() {
1352:                    return displayName;
1353:                }
1354:
1355:                public int compareTo(Object o) {
1356:                    final DatabasePool pool = (DatabasePool) o;
1357:                    int names = name.compareTo(pool.name);
1358:                    if (applicationName == null) {
1359:                        if (pool.applicationName == null) {
1360:                            return names;
1361:                        } else {
1362:                            return -1;
1363:                        }
1364:                    } else {
1365:                        if (pool.applicationName == null) {
1366:                            return 1;
1367:                        } else {
1368:                            int test = applicationName
1369:                                    .compareTo(pool.applicationName);
1370:                            if (test != 0) {
1371:                                return test;
1372:                            } else {
1373:                                return names;
1374:                            }
1375:                        }
1376:                    }
1377:                }
1378:            }
1379:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.