| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivelock; |
| 16 | |
| 17 | import java.security.Principal; |
| 18 | |
| 19 | // singleton |
| 20 | public class DefaultPrincipalHelperImpl implements PrincipalHelperService |
| 21 | { |
| 22 | public String getLogin(Principal userPrincipal) |
| 23 | { |
| 24 | return getPrincipal(userPrincipal).getLogin(); |
| 25 | } |
| 26 | |
| 27 | public String getName(Principal userPrincipal) |
| 28 | { |
| 29 | return getPrincipal(userPrincipal).getName(); |
| 30 | } |
| 31 | |
| 32 | public String[] getRoles(Principal userPrincipal) |
| 33 | { |
| 34 | return getPrincipal(userPrincipal).getRoles(); |
| 35 | } |
| 36 | |
| 37 | private DefaultPrincipal getPrincipal(Principal userPrincipal) |
| 38 | { |
| 39 | if (userPrincipal == null) |
| 40 | { |
| 41 | throw new SecurityException("Principal argument must not be null"); |
| 42 | } |
| 43 | if (userPrincipal instanceof DefaultPrincipal) |
| 44 | { |
| 45 | return (DefaultPrincipal) userPrincipal; |
| 46 | } |
| 47 | throw new SecurityException("Unknown Principal class " + userPrincipal.getClass()); |
| 48 | } |
| 49 | } |