View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.impl;
18  
19  import org.apache.jetspeed.security.BasePrincipal;
20  
21  /***
22   * <p>
23   * {@link BasePrincipal} interface implementation.
24   * </p>
25   * 
26   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
27   */
28  public abstract class BasePrincipalImpl implements BasePrincipal
29  {
30      
31      /*** The version uid. */
32      private static final long serialVersionUID = 5687385387290144541L;
33  
34      /*** The principal name. */
35      private final String name;
36  
37      /*** The full path. */
38      private final String fullPath;
39  
40      /***
41       * <p>
42       * Principal constructor given a name and preferences root.
43       * </p>
44       * 
45       * @param name The principal name.
46       * @param prefsRoot The preferences root node.
47       */
48      public BasePrincipalImpl(String name, String prefsRoot, boolean hiearchicalNames)
49      {
50          this.name = name;
51          this.fullPath = getFullPathFromPrincipalName(name, prefsRoot, hiearchicalNames);
52      }
53  
54      /***
55       * @see org.apache.jetspeed.security.BasePrincipal#getFullPath()
56       */
57      public String getFullPath()
58      {
59          return this.fullPath;
60      }
61  
62      /***
63       * @see java.security.Principal#getName()
64       */
65      public String getName()
66      {
67          return this.name;
68      }
69  
70      /***
71       * @see java.lang.Object#hashCode()
72       */
73      public int hashCode()
74      {
75          return this.name.hashCode();
76      }
77  
78      /***
79       * <p>
80       * Returns a string representation of this principal.
81       * </p>
82       * 
83       * @return A string representation of this principal.
84       */
85      public String toString()
86      {
87          return this.name;
88      }
89  
90      /***
91       * <p>
92       * Gets the principal implementation full path from the principal name.
93       * </p>
94       * <p>
95       * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
96       * separator for hierarchical elements.
97       * </p>
98       * <p>
99       * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
100      * </p>
101      * 
102      * @param name The principal name.
103      * @param prefsRoot The preferences root node.
104      * @param hiearchicalNames indicator if hierarchy encoding (replacing '.' with '/') should be done
105      * @return The preferences full path / principal name.
106      */
107     public static String getFullPathFromPrincipalName(String name, String prefsRoot, boolean hiearchicalNames)
108     {
109         String fullPath = name;
110         if (null != name )
111         {
112             fullPath = prefsRoot + (hiearchicalNames ? name.replace('.','/') : name );
113         }
114         return fullPath;
115     }
116 
117     /***
118      * <p>
119      * Gets the principal implementation full path from the principal name.
120      * </p>
121      * <p>
122      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
123      * separator for hierarchical elements.
124      * </p>
125      * <p>
126      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
127      * </p>
128      * 
129      * @param name The principal name.
130      * @param prefsRoot The preferences root node.
131      * @return The preferences full path / principal name.
132      */        
133 
134     /***
135      * <p>
136      * Gets the principal name from the principal implementation full path.
137      * </p>
138      * <p>
139      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
140      * separator for hierarchical elements.
141      * </p>
142      * <p>
143      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
144      * </p>
145      * 
146      * @param fullPath The principal full path.
147      * @param prefsRoot The preferences root node.
148      * @param hiearchicalNames indicator if hierarchical decoding (replacing '/' with '.') should be done
149      * @return The principal name.
150      */
151     public static String getPrincipalNameFromFullPath(String fullPath, String prefsRoot, boolean hiearchicalNames)
152     {
153         String name = fullPath;
154         if (null != name)
155         {
156             name = name.substring(prefsRoot.length(), name.length());
157             if ( hiearchicalNames )
158             {
159                 name = name.replace('/', '.');
160             }
161         }
162         return name;
163     }
164 
165     /***
166      * <p>
167      * Gets the principal name from the principal implementation full path.
168      * </p>
169      * <p>
170      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
171      * separator for hierarchical elements.
172      * </p>
173      * <p>
174      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
175      * </p>
176      * 
177      * @param fullPath The principal full path.
178      * @param prefsRoot The preferences root node.
179      * @return The principal name.
180      */
181 // MOVED TO DERVICED CLASSES    
182 //    public static String getPrincipalNameFromFullPath(String fullPath, String prefsRoot)
183 //    {
184 //        return getPrincipalNameFromFullPath(fullPath, prefsRoot, true);
185 //    }
186 
187     private boolean enabled = true;
188 
189     /***
190      * @see org.apache.jetspeed.security.BasePrincipal#isEnabled()
191      */
192     public boolean isEnabled()
193     {
194         return enabled;
195     }
196 
197     /***
198      * @see org.apache.jetspeed.security.BasePrincipal#setEnabled(boolean)
199      */
200     public void setEnabled(boolean enabled)
201     {
202         this.enabled = enabled;
203     }
204 }