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.jetspeed.security.impl;
018:
019: import org.apache.jetspeed.security.BasePrincipal;
020:
021: /**
022: * <p>
023: * {@link BasePrincipal} interface implementation.
024: * </p>
025: *
026: * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
027: */
028: public abstract class BasePrincipalImpl implements BasePrincipal {
029:
030: /** The version uid. */
031: private static final long serialVersionUID = 5687385387290144541L;
032:
033: /** The principal name. */
034: private final String name;
035:
036: /** The full path. */
037: private final String fullPath;
038:
039: /** is this principal enabled **/
040: private boolean enabled = true;
041:
042: /** is this principal a mapping **/
043: private boolean isMapping = false;
044:
045: /**
046: * <p>
047: * Principal constructor given a name and preferences root.
048: * </p>
049: *
050: * @param name The principal name.
051: * @param prefsRoot The preferences root node.
052: */
053: public BasePrincipalImpl(String name, String prefsRoot,
054: boolean hiearchicalNames) {
055: this (name, prefsRoot, hiearchicalNames, true, false);
056: }
057:
058: public BasePrincipalImpl(String name, String prefsRoot,
059: boolean hiearchicalNames, boolean isEnabled,
060: boolean isMapping) {
061: this .name = name;
062: this .fullPath = getFullPathFromPrincipalName(name, prefsRoot,
063: hiearchicalNames);
064: this .enabled = isEnabled;
065: this .isMapping = isMapping;
066: }
067:
068: /**
069: * @see org.apache.jetspeed.security.BasePrincipal#getFullPath()
070: */
071: public String getFullPath() {
072: return this .fullPath;
073: }
074:
075: /**
076: * @see java.security.Principal#getName()
077: */
078: public String getName() {
079: return this .name;
080: }
081:
082: /**
083: * @see java.lang.Object#hashCode()
084: */
085: public int hashCode() {
086: return this .name.hashCode();
087: }
088:
089: /**
090: * <p>
091: * Returns a string representation of this principal.
092: * </p>
093: *
094: * @return A string representation of this principal.
095: */
096: public String toString() {
097: return this .name;
098: }
099:
100: /**
101: * <p>
102: * Gets the principal implementation full path from the principal name.
103: * </p>
104: * <p>
105: * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
106: * separator for hierarchical elements.
107: * </p>
108: * <p>
109: * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
110: * </p>
111: *
112: * @param name The principal name.
113: * @param prefsRoot The preferences root node.
114: * @param hiearchicalNames indicator if hierarchy encoding (replacing '.' with '/') should be done
115: * @return The preferences full path / principal name.
116: */
117: public static String getFullPathFromPrincipalName(String name,
118: String prefsRoot, boolean hiearchicalNames) {
119: String fullPath = name;
120: if (null != name) {
121: fullPath = prefsRoot
122: + (hiearchicalNames ? name.replace('.', '/') : name);
123: }
124: return fullPath;
125: }
126:
127: /**
128: * <p>
129: * Gets the principal implementation full path from the principal name.
130: * </p>
131: * <p>
132: * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
133: * separator for hierarchical elements.
134: * </p>
135: * <p>
136: * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
137: * </p>
138: *
139: * @param name The principal name.
140: * @param prefsRoot The preferences root node.
141: * @return The preferences full path / principal name.
142: */
143:
144: /**
145: * <p>
146: * Gets the principal name from the principal implementation full path.
147: * </p>
148: * <p>
149: * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
150: * separator for hierarchical elements.
151: * </p>
152: * <p>
153: * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
154: * </p>
155: *
156: * @param fullPath The principal full path.
157: * @param prefsRoot The preferences root node.
158: * @param hiearchicalNames indicator if hierarchical decoding (replacing '/' with '.') should be done
159: * @return The principal name.
160: */
161: public static String getPrincipalNameFromFullPath(String fullPath,
162: String prefsRoot, boolean hiearchicalNames) {
163: String name = fullPath;
164: if (null != name) {
165: name = name.substring(prefsRoot.length(), name.length());
166: if (hiearchicalNames) {
167: name = name.replace('/', '.');
168: }
169: }
170: return name;
171: }
172:
173: /**
174: * <p>
175: * Gets the principal name from the principal implementation full path.
176: * </p>
177: * <p>
178: * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
179: * separator for hierarchical elements.
180: * </p>
181: * <p>
182: * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
183: * </p>
184: *
185: * @param fullPath The principal full path.
186: * @param prefsRoot The preferences root node.
187: * @return The principal name.
188: */
189: // MOVED TO DERVICED CLASSES
190: // public static String getPrincipalNameFromFullPath(String fullPath, String prefsRoot)
191: // {
192: // return getPrincipalNameFromFullPath(fullPath, prefsRoot, true);
193: // }
194: /**
195: * @see org.apache.jetspeed.security.BasePrincipal#isEnabled()
196: */
197: public boolean isEnabled() {
198: return enabled;
199: }
200:
201: /**
202: * @see org.apache.jetspeed.security.BasePrincipal#setEnabled(boolean)
203: */
204: public void setEnabled(boolean enabled) {
205: this .enabled = enabled;
206: }
207:
208: public boolean isMapping() {
209: return isMapping;
210: }
211:
212: }
|