001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Tiago Cardoso <tcardoso@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.threed.convert;
020
021import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
022import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
023import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
024import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
025import org.nuxeo.runtime.api.Framework;
026
027/**
028 * Helper class to get java runtime owner user id
029 *
030 * @since 8.4
031 */
032public class UserIdHelper {
033
034    public static final String WINDOWS = "Windows";
035
036    public static final String USER_ID_COMMAND = "useruid";
037
038    public static final String USER_PARAM = "username";
039
040    protected static String UID;
041
042    public static String getUid() {
043        if (UID == null) {
044            if (!WINDOWS.equals(System.getProperty("os.name"))) {
045                CmdParameters params = new CmdParameters();
046                params.addNamedParameter(USER_PARAM, System.getProperty("user.name"));
047                ExecResult result = null;
048                try {
049                    result = Framework.getService(CommandLineExecutorService.class).execCommand(USER_ID_COMMAND,
050                            params);
051                    UID = result.getOutput().get(0);
052                } catch (CommandNotAvailable commandNotAvailable) {
053                    UID = "";
054                }
055            } else {
056                UID = "";
057            }
058        }
059        return UID;
060    }
061}