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