aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/explorer/commands/shell/IdentityCommand.java
blob: 0d741f4ba6cf81ccc668ba00e9b456d575ef0386 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed 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.cyanogenmod.explorer.commands.shell;

import com.cyanogenmod.explorer.commands.IdentityExecutable;
import com.cyanogenmod.explorer.console.CommandNotFoundException;
import com.cyanogenmod.explorer.console.ExecutionException;
import com.cyanogenmod.explorer.console.InsufficientPermissionsException;
import com.cyanogenmod.explorer.model.AID;
import com.cyanogenmod.explorer.model.Group;
import com.cyanogenmod.explorer.model.Identity;
import com.cyanogenmod.explorer.model.User;
import com.cyanogenmod.explorer.util.FileHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


/**
 * A class for retrieve identity information of the current user
 * (user associated to the active shell).
 *
 * {@link "http://unixhelp.ed.ac.uk/CGI/man-cgi?id"}
 */
//
//<user>          <group>         <groups>
//uid=2000(shell) gid=2000(shell) groups=1003(graphics),1004(input),...
//
public class IdentityCommand extends SyncResultProgram implements IdentityExecutable {

    private static final String ID = "id";  //$NON-NLS-1$
    private Identity mIdentity;

    private static final String UID = "uid";  //$NON-NLS-1$
    private static final String GID = "gid";  //$NON-NLS-1$
    private static final String GROUPS = "groups";  //$NON-NLS-1$

    /**
     * Constructor of <code>IdentityCommand</code>.
     *
     * @throws InvalidCommandDefinitionException If the command has an invalid definition
     */
    public IdentityCommand() throws InvalidCommandDefinitionException {
        super(ID);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void parse(String in, String err) throws ParseException {
        //Release the return object
        this.mIdentity = null;

        // Check the in buffer to extract information
        BufferedReader br = null;
        try {
            br = new BufferedReader(new StringReader(in));
            String szLine = br.readLine();
            if (szLine == null) {
                throw new ParseException("no information", 0); //$NON-NLS-1$
            }

            szLine = szLine.replaceAll(" ", FileHelper.NEWLINE); //$NON-NLS-1$
            Properties p = new Properties();
            p.load(new StringReader(szLine));

            //At least uid and gid must be present
            if (!p.containsKey(UID) && !p.containsKey(GID)) {
                throw new ParseException(
                        String.format("no %s or %s present", UID, GID), 0); //$NON-NLS-1$
            }

            //1.- Extract user
            User user = (User)createAID(p.getProperty(UID), User.class);

            //2.- Extract group
            Group group = (Group)createAID(p.getProperty(GID), Group.class);

            //3.- Extract groups
            List<Group> groups = new ArrayList<Group>();
            String szGroups = p.getProperty(GROUPS);
            if (szGroups != null && szGroups.length() > 0) {
                String[] aGroups = szGroups.split(","); //$NON-NLS-1$
                int cc = aGroups.length;
                for (int i = 0; i < cc; i++) {
                    groups.add((Group)createAID(aGroups[i], Group.class));
                }
            }

            //Now the string is parsed into a reference
            this.mIdentity = new Identity(user, group, groups);

        } catch (IOException ioEx) {
            throw new ParseException(ioEx.getMessage(), 0);

        } catch (ParseException pEx) {
            throw pEx;

        } catch (Exception ex) {
            throw new ParseException(ex.getMessage(), 0);

        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (Throwable ex) {
                /**NON BLOCK**/
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Identity getResult() {
        return this.mIdentity;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void checkExitCode(int exitCode)
            throws InsufficientPermissionsException, CommandNotFoundException, ExecutionException {
        if (exitCode != 0) {
            throw new ExecutionException("exitcode != 0"); //$NON-NLS-1$
        }
    }

    /**
     * Method that creates the {@link AID} from the parsed string.
     *
     * @param src The string to parsed into a {@link AID}
     * @param clazz The {@link User} or {@link Group} class from which create the AID object
     * @return AID The identity reference
     * @throws ParseException If can't create the {@link AID} reference from the string
     * @throws NoSuchMethodException If the constructor can not be found.
     * @exception InstantiationException If the class cannot be instantiated
     * @exception IllegalAccessException If this constructor is not accessible
     * @exception InvocationTargetException If an exception was thrown by the invoked constructor
     */
    @SuppressWarnings({ "unchecked", "static-method", "boxing" })
    private AID createAID(String src, Class<?> clazz)
            throws ParseException, NoSuchMethodException,
            InstantiationException, IllegalAccessException, InvocationTargetException {
        int id = 0;
        try {
            id = Integer.parseInt(src.substring(0, src.lastIndexOf('(')).trim());
        } catch (NumberFormatException nfEx) {
            throw new ParseException(String.format("not valid AID id: %s", src), 0); //$NON-NLS-1$
        }
        String szName = src.substring(src.lastIndexOf('(') + 1, src.lastIndexOf(')'));

        Constructor<AID> constructor =
                (Constructor<AID>)clazz.getConstructor(Integer.TYPE, String.class);
        return constructor.newInstance(id, szName);
    }

}