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