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.service;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.oozie.util.XConfiguration;
022
023import java.io.IOException;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * The GroupsService class delegates to the Hadoop's <code>org.apache.hadoop.security.Groups</code>
029 * to retrieve the groups a user belongs to.
030 */
031public class GroupsService implements Service {
032    public static final String CONF_PREFIX = Service.CONF_PREFIX + "GroupsService.";
033
034    private org.apache.hadoop.security.Groups hGroups;
035
036    /**
037     * Returns the service interface.
038     * @return <code>GroupService</code>
039     */
040    @Override
041    public Class<? extends Service> getInterface() {
042        return GroupsService.class;
043    }
044
045    /**
046     * Initializes the service.
047     *
048     * @param services services singleton initializing the service.
049     */
050    @Override
051    public void init(Services services) {
052        Configuration sConf = services.getConf();
053        Configuration gConf = new XConfiguration();
054        for (Map.Entry<String, String> entry : sConf) {
055            String name = entry.getKey();
056            if (name.startsWith(CONF_PREFIX)) {
057                gConf.set(name.substring(CONF_PREFIX.length()), sConf.get(name));
058            }
059        }
060        hGroups = new org.apache.hadoop.security.Groups(gConf);
061    }
062
063    /**
064     * Destroys the service.
065     */
066    @Override
067    public void destroy() {
068    }
069
070    /**
071     * Returns the list of groups a user belongs to.
072     *
073     * @param user user name.
074     * @return the groups the given user belongs to.
075     * @throws IOException thrown if there was an error retrieving the groups of the user.
076     */
077    public List<String> getGroups(String user) throws IOException {
078        return hGroups.getGroups(user);
079    }
080
081}