001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.security.deploy;
019    
020    import java.beans.PropertyEditorManager;
021    
022    import org.apache.geronimo.common.propertyeditor.PropertyEditorException;
023    import org.apache.geronimo.common.propertyeditor.TextPropertyEditorSupport;
024    
025    
026    /**
027     * @version $Rev: 470785 $ $Date: 2006-11-03 04:44:30 -0800 (Fri, 03 Nov 2006) $
028     */
029    public class RealmPrincipalInfo extends LoginDomainPrincipalInfo {
030    
031        static {
032            PropertyEditorManager.registerEditor(RealmPrincipalInfo.class, RealmPrincipalEditor.class);
033        }
034    
035        private final String realm;
036    
037        public RealmPrincipalInfo(String realm, String domainName, String className, String principalName, boolean designatedRunAs) {
038            super(domainName, className, principalName, designatedRunAs);
039            this.realm = realm;
040        }
041    
042        public String getRealm() {
043            return realm;
044        }
045    
046        public static class RealmPrincipalEditor extends TextPropertyEditorSupport {
047    
048            public void setAsText(String text) {
049                if (text != null) {
050                    String[] parts = text.split(",");
051                    if (parts.length != 5) {
052                        throw new PropertyEditorException("Principal should have the form 'realm,domain,class,name,run-as'");
053                    }
054                    RealmPrincipalInfo principal = new RealmPrincipalInfo(parts[0], parts[1], parts[2], parts[3], Boolean.valueOf(parts[4]).booleanValue());
055                    setValue(principal);
056                } else {
057                    setValue(null);
058                }
059            }
060    
061            public String getAsText() {
062                RealmPrincipalInfo principal = (RealmPrincipalInfo) getValue();
063                if (principal == null) {
064                    return null;
065                }
066                // output from getAsText() should match with the input expected by setAsText()
067                return principal.getRealm() + "," + principal.getDomain() + "," + principal.getClassName() + "," + principal.getPrincipalName() + "," + principal.isDesignatedRunAs();
068            }
069        }
070    }