|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.auth; |
| 18 | + |
| 19 | +import com.cloud.utils.component.ComponentContext; |
| 20 | +import com.cloud.utils.component.ManagerBase; |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 22 | +import org.apache.log4j.Logger; |
| 23 | + |
| 24 | +import javax.ejb.Local; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +@Local(value = APIAuthenticationManager.class) |
| 31 | +@SuppressWarnings("unchecked") |
| 32 | +public class APIAuthenticationManagerImpl extends ManagerBase implements APIAuthenticationManager { |
| 33 | + public static final Logger s_logger = Logger.getLogger(APIAuthenticationManagerImpl.class.getName()); |
| 34 | + |
| 35 | + private static Map<String, Class<?>> s_authenticators = null; |
| 36 | + private static List<Class<?>> s_commandList = null; |
| 37 | + |
| 38 | + public APIAuthenticationManagerImpl() { |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean start() { |
| 43 | + s_authenticators = new HashMap<String, Class<?>>(); |
| 44 | + for (Class<?> authenticator: getCommands()) { |
| 45 | + APICommand command = authenticator.getAnnotation(APICommand.class); |
| 46 | + if (command != null && !command.name().isEmpty() |
| 47 | + && APIAuthenticator.class.isAssignableFrom(authenticator)) { |
| 48 | + s_authenticators.put(command.name(), authenticator); |
| 49 | + } |
| 50 | + } |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public List<Class<?>> getCommands() { |
| 56 | + if (s_commandList == null) { |
| 57 | + s_commandList = new ArrayList<Class<?>>(); |
| 58 | + s_commandList.add(DefaultLoginAPIAuthenticatorCmd.class); |
| 59 | + s_commandList.add(DefaultLogoutAPIAuthenticatorCmd.class); |
| 60 | + } |
| 61 | + return s_commandList; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public APIAuthenticator getAPIAuthenticator(String name) { |
| 66 | + APIAuthenticator apiAuthenticator = null; |
| 67 | + if (s_authenticators != null && s_authenticators.containsKey(name)) { |
| 68 | + try { |
| 69 | + apiAuthenticator = (APIAuthenticator) s_authenticators.get(name).newInstance(); |
| 70 | + apiAuthenticator = ComponentContext.inject(apiAuthenticator); |
| 71 | + } catch (InstantiationException | IllegalAccessException e) { |
| 72 | + if (s_logger.isDebugEnabled()) { |
| 73 | + s_logger.debug("APIAuthenticationManagerImpl::getAPIAuthenticator failed: " + e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + return apiAuthenticator; |
| 78 | + } |
| 79 | +} |
0 commit comments