001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.action.hadoop;
016    
017    import org.apache.hadoop.conf.Configuration;
018    import org.apache.hadoop.util.ReflectionUtils;
019    import org.apache.oozie.service.Services;
020    import org.apache.oozie.util.XLog;
021    
022    public class CredentialsProvider {
023        Credentials cred;
024        String type;
025        private static final String CRED_KEY = "oozie.credentials.credentialclasses";
026        private static final XLog LOG = XLog.getLog(CredentialsProvider.class);
027    
028        public CredentialsProvider(String type) {
029            this.type = type;
030            this.cred = null;
031            LOG.debug("Credentials Provider is created for Type: " + type);
032        }
033    
034        /**
035         * Create Credential object
036         *
037         * @return Credential object
038         * @throws Exception
039         */
040        public Credentials createCredentialObject() throws Exception {
041            Configuration conf;
042            String type;
043            String classname;
044            conf = Services.get().getConf();
045            if (conf.get(CRED_KEY, "").trim().length() > 0) {
046                for (String function : conf.getStrings(CRED_KEY)) {
047                    function = Trim(function);
048                    LOG.debug("Creating Credential class for : " + function);
049                    String[] str = function.split("=");
050                    if (str.length > 0) {
051                        type = str[0];
052                        classname = str[1];
053                        if (classname != null) {
054                            LOG.debug("Creating Credential type : '" + type + "', class Name : '" + classname + "'");
055                            if (this.type.equalsIgnoreCase(str[0])) {
056                                Class<?> klass = null;
057                                try {
058                                    klass = Thread.currentThread().getContextClassLoader().loadClass(classname);
059                                }
060                                catch (ClassNotFoundException ex) {
061                                    LOG.warn("Exception while loading the class", ex);
062                                    throw ex;
063                                }
064    
065                                cred = (Credentials) ReflectionUtils.newInstance(klass, null);
066                            }
067                        }
068                    }
069                }
070            }
071            return cred;
072        }
073    
074        /**
075         * To trim string
076         *
077         * @param str
078         * @return trim string
079         */
080        public String Trim(String str) {
081            if (str != null) {
082                str = str.replaceAll("\\n", "");
083                str = str.replaceAll("\\t", "");
084                str = str.trim();
085            }
086            return str;
087        }
088    }