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