001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 1997-2010 Oracle and/or its affiliates. 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 Development
008 * and Distribution License("CDDL") (collectively, the "License"). You
009 * may not use this file except in compliance with the License. You can
010 * obtain a copy of the License at
011 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
012 * or packager/legal/LICENSE.txt. See the License for the specific
013 * language governing permissions and limitations under the License.
014 *
015 * When distributing the software, include this License Header Notice in each
016 * file and include the License file at packager/legal/LICENSE.txt.
017 *
018 * GPL Classpath Exception:
019 * Oracle designates this particular file as subject to the "Classpath"
020 * exception as provided by Oracle in the GPL Version 2 section of the License
021 * file that accompanied this code.
022 *
023 * Modifications:
024 * If applicable, add the following below the License Header, with the fields
025 * enclosed by brackets [] replaced by your own identifying information:
026 * "Portions Copyright [year] [name of copyright owner]"
027 *
028 * Contributor(s):
029 * If you wish your version of this file to be governed by only the CDDL or
030 * only the GPL Version 2, indicate your decision by adding "[Contributor]
031 * elects to include this software in this distribution under the [CDDL or GPL
032 * Version 2] license." If you don't indicate a single choice of license, a
033 * recipient has the option to distribute your version of this file under
034 * either the CDDL, the GPL Version 2 or to extend the choice of license to
035 * its licensees as provided above. However, if you add GPL Version 2 code
036 * and therefore, elected the GPL Version 2 license, then the option applies
037 * only if the new code is made subject to such option by the copyright
038 * holder.
039 */
040
041 package com.sun.enterprise.admin.cli;
042
043 import java.io.File;
044 import java.io.FileFilter;
045 import java.util.List;
046 import java.util.ArrayList;
047 import java.util.Iterator;
048
049 /**
050 * Builds up a classpath.
051 *
052 * @author Kohsuke Kawaguchi
053 */
054 public class ClassPathBuilder implements Iterable<File> {
055 private final List<File> elements = new ArrayList<File>();
056
057 public Iterator<File> iterator() {
058 return elements.iterator();
059 }
060
061 /**
062 * Adds a single jar file or a class file directory.
063 */
064 public ClassPathBuilder add(File f) {
065 elements.add(f);
066 return this;
067 }
068
069 /**
070 * Allows one to write {@code add(f,"lib","a.jar")} instead of
071 * <tt>add(new File(new File(f,"lib"),"a.jar")</tt>
072 */
073 public ClassPathBuilder add(File f, String... pathFragments) {
074 for (String p : pathFragments)
075 f = new File(f,p);
076 return add(f);
077 }
078
079 /**
080 * Adds all the files in the given directory that match the given filter.
081 */
082 public ClassPathBuilder addAll(File dir, FileFilter filter) {
083 File[] files = dir.listFiles(filter);
084 if(files!=null)
085 addAll(files);
086 return this;
087 }
088
089 public ClassPathBuilder addAll(File... files) {
090 for (File f : files)
091 add(f);
092 return this;
093 }
094
095 /**
096 * Formats the path in a single-argument format suitable
097 * after the "-cp" JVM option.
098 */
099 public String toString() {
100 StringBuilder buf = new StringBuilder();
101 for (File f : elements) {
102 if(buf.length()>0) buf.append(File.pathSeparatorChar);
103 // this method is normally used to create an argument for another process,
104 // so better resolve relative path to absolute path.
105 buf.append(f.getAbsolutePath());
106 }
107 return buf.toString();
108 }
109 }