001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.mail.util;
022:
023: import com.liferay.portal.kernel.util.ProcessUtil;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.util.PropsUtil;
028:
029: import java.util.List;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <a href="ShellHook.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Michael Lawrence
038: *
039: */
040: public class ShellHook implements Hook {
041:
042: public static String SHELL_SCRIPT = PropsUtil
043: .get(PropsUtil.MAIL_HOOK_SHELL_SCRIPT);
044:
045: public void addFilters(long userId, List filters) {
046: }
047:
048: public void addForward(long userId, List filters,
049: List emailAddresses, boolean leaveCopy) {
050:
051: _execute(new String[] { SHELL_SCRIPT, "addForward",
052: String.valueOf(userId),
053: StringUtil.merge(emailAddresses) });
054: }
055:
056: public void addUser(long userId, String password, String firstName,
057: String middleName, String lastName, String emailAddress) {
058:
059: _execute(new String[] { SHELL_SCRIPT, "addUser",
060: String.valueOf(userId), password, firstName,
061: middleName, lastName, emailAddress });
062: }
063:
064: public void addVacationMessage(long userId, String emailAddress,
065: String vacationMessage) {
066:
067: _execute(new String[] { SHELL_SCRIPT, "addVacationMessage",
068: String.valueOf(userId), emailAddress, vacationMessage });
069: }
070:
071: public void deleteEmailAddress(long userId) {
072: _execute(new String[] { SHELL_SCRIPT, "deleteEmailAddress",
073: String.valueOf(userId) });
074: }
075:
076: public void deleteUser(long userId) {
077: _execute(new String[] { SHELL_SCRIPT, "deleteUser",
078: String.valueOf(userId) });
079: }
080:
081: public void updateBlocked(long userId, List blocked) {
082: _execute(new String[] { SHELL_SCRIPT, "updateBlocked",
083: String.valueOf(userId), StringUtil.merge(blocked) });
084: }
085:
086: public void updateEmailAddress(long userId, String emailAddress) {
087: _execute(new String[] { SHELL_SCRIPT, "updateEmailAddress",
088: String.valueOf(userId), emailAddress });
089: }
090:
091: public void updatePassword(long userId, String password) {
092: _execute(new String[] { SHELL_SCRIPT, "updatePassword",
093: String.valueOf(userId), password });
094: }
095:
096: private void _execute(String cmdLine[]) {
097: for (int i = 0; i < cmdLine.length; i++) {
098: if (cmdLine[i].trim().length() == 0) {
099: cmdLine[i] = StringPool.UNDERLINE;
100: }
101: }
102:
103: try {
104: Runtime rt = Runtime.getRuntime();
105:
106: Process p = rt.exec(cmdLine);
107:
108: ProcessUtil.close(p);
109:
110: int exitValue = p.exitValue();
111:
112: if (exitValue != 0) {
113: StringMaker cmd = new StringMaker();
114:
115: for (int i = 0; i < cmdLine.length; i++) {
116: cmd.append(cmdLine[i]);
117: cmd.append(StringPool.SPACE);
118: }
119:
120: throw new IllegalArgumentException(cmd.toString());
121: }
122: } catch (Exception e) {
123: _log.error(e);
124: }
125: }
126:
127: private static Log _log = LogFactory.getLog(ShellHook.class);
128:
129: }
|