001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.php.rt.providers.impl.local.apache;
042:
043: import java.io.BufferedReader;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import java.util.ArrayList;
049: import java.util.Collection;
050: import java.util.HashSet;
051: import java.util.LinkedList;
052: import java.util.List;
053: import java.util.Set;
054: import java.util.logging.Logger;
055:
056: /**
057: * @author ads
058: *
059: */
060: class SolarisPackageFinder {
061:
062: static final String PKGINFO = "pkginfo"; // NOI18N
063:
064: static final String GREP = "grep"; // NOI18N
065:
066: static final String USR_BIN = "/usr/bin"; // NOI18N
067:
068: static final String WHICH = "which"; // NOI18N
069:
070: static final String CATEGORY = "application";// NOI18N
071:
072: static final String PKG_KEY = "apache"; // NOI18N
073:
074: static final String SADM_CONTENTS = "/var/sadm/install/contents"; // NOI18N
075:
076: private static final String SXDE_CONF_LOCATION = "/etc/apache2/2.2/httpd.conf"; // NOI18N
077:
078: private static Logger LOGGER = Logger
079: .getLogger(SolarisPackageFinder.class.getName());
080:
081: public String[] getPlatformLocations() {
082: if (!whichExists()) {
083: return null;
084: }
085: Collection<String> pkgNames = getPkgNames();
086: if (pkgNames == null) {
087: return null;
088: }
089: List<String> list = new ArrayList<String>();
090: for (String name : pkgNames) {
091: Collection<String> location = getConfigsLocations(name);
092: list.addAll(location);
093: }
094: // hack for SXDE to show default config at the top
095: if (list.contains(SXDE_CONF_LOCATION)) {
096: list.remove(SXDE_CONF_LOCATION);
097: list.add(0, SXDE_CONF_LOCATION);
098: }
099: return list.toArray(new String[list.size()]);
100: }
101:
102: private Collection<String> getConfigsLocations(String name) {
103: String grep = getGrep();
104: Runtime runtime = Runtime.getRuntime();
105: List<String> list = new ArrayList<String>();
106: try {
107: Process process = runtime.exec(new String[] { grep, name,
108: SADM_CONTENTS });
109: LOGGER.info(">>>> " + grep + " " + name + " in "
110: + SADM_CONTENTS);
111: InputStream stream = process.getInputStream();
112: BufferedReader reader = new BufferedReader(
113: new InputStreamReader(stream));
114: String line;
115: while ((line = reader.readLine()) != null) {
116: /* Do not use PATH_TO_HTTPD_CONF because it's place in SAMP
117: * is differnt.
118: */
119: //String httpdConf = ServerChooserVisual.PATH_TO_HTTPD_CONF;
120: String httpdConf = ServerChooserVisual.HTTPD_CONF;
121: if (line.contains(httpdConf)) {
122: LOGGER.info(">>>>> line contains httpd");
123: line = line.replace('\t', ' ');
124: int httpdIndex = line.indexOf(httpdConf);
125: int locationEndIndex = line
126: .indexOf(' ', httpdIndex);
127: // if name is 'httpd.conf', not e.g. 'httpd.conf-example'
128: if (locationEndIndex == httpdIndex
129: + httpdConf.length()) {
130: list.add(line.substring(0, locationEndIndex));
131: }
132: }
133: }
134: } catch (IOException e) {
135: // just do not fill list with locations
136: }
137: return list;
138: }
139:
140: private boolean whichExists() {
141: String which = getProgramPath(WHICH);
142: if (which != null) {
143: myWhich = which;
144: return true;
145: } else {
146: // try to check which in specified place
147: myWhich = USR_BIN + File.separator + WHICH;
148: myWhich = getProgramPath(myWhich);
149: if (myWhich == null) {
150: return false;
151: } else {
152: return true;
153: }
154: }
155: }
156:
157: private String getPkgInfo() {
158: if (myPkgInfo == null) {
159: myPkgInfo = getProgramPath(PKGINFO);
160: if (myPkgInfo == null) {
161: myPkgInfo = getProgramPath(USR_BIN + File.separator
162: + PKGINFO);
163: }
164: }
165: return myPkgInfo;
166: }
167:
168: private String getGrep() {
169: if (myGrep == null) {
170: myGrep = getProgramPath(GREP);
171: if (myGrep == null) {
172: myGrep = getProgramPath(USR_BIN + File.separator + GREP);
173: }
174: }
175: return myGrep;
176: }
177:
178: private Collection<String> getPkgNames() {
179: Runtime runtime = Runtime.getRuntime();
180: String pkgInfo = getPkgInfo();
181: String grep = getGrep();
182: if (pkgInfo == null || grep == null) {
183: return null;
184: }
185: try {
186: /* do not filter by category
187: * because it is different in different solaris versions
188: * system if installed as SAMP with SXDE, application otherwise.
189: * -c application,system has no sence because number of result
190: * is the same as without category.
191: */
192: //Process process = runtime.exec( pkgInfo +" -c " +CATEGORY + " -i "); // NOI18N
193: Process process = runtime.exec(pkgInfo + " -i "); // NOI18N
194: BufferedReader reader = new BufferedReader(
195: new InputStreamReader(process.getInputStream()));
196: String line;
197: Collection<String> collection = new LinkedList<String>();
198: while ((line = reader.readLine()) != null) {
199: if (!line.toLowerCase().contains(PKG_KEY)) {
200: continue;
201: }
202: line = cutPkgName(line);
203: if (line != null) {
204: collection.add(line);
205: }
206: }
207: return collection;
208: } catch (IOException e) {
209: return null;
210: }
211: }
212:
213: private String getProgramPath(String program) {
214: Runtime runtime = Runtime.getRuntime();
215: String which = myWhich == null ? WHICH : myWhich;
216: try {
217: Process process = runtime.exec(new String[] { which,
218: program });
219: BufferedReader reader = new BufferedReader(
220: new InputStreamReader(process.getInputStream()));
221: String line;
222: while ((line = reader.readLine()) != null) {
223: if (line.endsWith(program)) {
224: return line;
225: }
226: }
227: return null;
228: } catch (IOException e) {
229: return null;
230: }
231: }
232:
233: private static String cutPkgName(String str) {
234: //if ( str.startsWith( CATEGORY )) {
235: str = str.replace('\t', ' ');
236: int index = str.indexOf(' ');
237: str = str.substring(index);
238: str = str.trim();
239: index = str.indexOf(' ');
240: str = str.substring(0, index);
241: return str.trim();
242: //}
243: //return null;
244: }
245:
246: private String myPkgInfo;
247:
248: private String myGrep;
249:
250: private String myWhich;
251: }
|