APIAuthenticationManagerImpl: add the auth manager and bean entry in spring xmls
- This implements ManageBase, is a pluggable service - Has a mechanism to return commands, useful for apidocs etc. - Has a method to return APIAuthenticator based on API command name Signed-off-by: Rohit Yadav <rohit.ya...@shapeblue.com> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/097211b5 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/097211b5 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/097211b5 Branch: refs/heads/auth-refactor Commit: 097211b5cdab72d297527b27ea719a664379e85b Parents: 92c5d1a Author: Rohit Yadav <rohit.ya...@shapeblue.com> Authored: Tue Aug 12 07:30:25 2014 +0200 Committer: Rohit Yadav <rohit.ya...@shapeblue.com> Committed: Tue Aug 12 09:19:43 2014 +0200 ---------------------------------------------------------------------- .../spring-server-core-managers-context.xml | 2 + .../api/auth/APIAuthenticationManagerImpl.java | 79 ++++++++++++++++++++ 2 files changed, 81 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/097211b5/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml ---------------------------------------------------------------------- diff --git a/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml b/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml index 1eba0b2..17681f7 100644 --- a/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml +++ b/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml @@ -32,6 +32,8 @@ http://www.springframework.org/schema/util/spring-util-3.0.xsd" > + <bean id="authenticationManagerImpl" class="com.cloud.api.auth.APIAuthenticationManagerImpl" /> + <bean id="accountManagerImpl" class="com.cloud.user.AccountManagerImpl"> <property name="userAuthenticators" value="#{userAuthenticatorsRegistry.registered}" /> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/097211b5/server/src/com/cloud/api/auth/APIAuthenticationManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/api/auth/APIAuthenticationManagerImpl.java b/server/src/com/cloud/api/auth/APIAuthenticationManagerImpl.java new file mode 100644 index 0000000..886d277 --- /dev/null +++ b/server/src/com/cloud/api/auth/APIAuthenticationManagerImpl.java @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.api.auth; + +import com.cloud.utils.component.ComponentContext; +import com.cloud.utils.component.ManagerBase; +import org.apache.cloudstack.api.APICommand; +import org.apache.log4j.Logger; + +import javax.ejb.Local; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Local(value = APIAuthenticationManager.class) +@SuppressWarnings("unchecked") +public class APIAuthenticationManagerImpl extends ManagerBase implements APIAuthenticationManager { + public static final Logger s_logger = Logger.getLogger(APIAuthenticationManagerImpl.class.getName()); + + private static Map<String, Class<?>> s_authenticators = null; + private static List<Class<?>> s_commandList = null; + + public APIAuthenticationManagerImpl() { + } + + @Override + public boolean start() { + s_authenticators = new HashMap<String, Class<?>>(); + for (Class<?> authenticator: getCommands()) { + APICommand command = authenticator.getAnnotation(APICommand.class); + if (command != null && !command.name().isEmpty() + && APIAuthenticator.class.isAssignableFrom(authenticator)) { + s_authenticators.put(command.name(), authenticator); + } + } + return true; + } + + @Override + public List<Class<?>> getCommands() { + if (s_commandList == null) { + s_commandList = new ArrayList<Class<?>>(); + s_commandList.add(DefaultLoginAPIAuthenticatorCmd.class); + s_commandList.add(DefaultLogoutAPIAuthenticatorCmd.class); + } + return s_commandList; + } + + @Override + public APIAuthenticator getAPIAuthenticator(String name) { + APIAuthenticator apiAuthenticator = null; + if (s_authenticators != null && s_authenticators.containsKey(name)) { + try { + apiAuthenticator = (APIAuthenticator) s_authenticators.get(name).newInstance(); + apiAuthenticator = ComponentContext.inject(apiAuthenticator); + } catch (InstantiationException | IllegalAccessException e) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("APIAuthenticationManagerImpl::getAPIAuthenticator failed: " + e.getMessage()); + } + } + } + return apiAuthenticator; + } +}