001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.plugin;
018    
019    import java.io.Serializable;
020    import java.net.URL;
021    import java.util.List;
022    import java.util.ArrayList;
023    import org.apache.geronimo.kernel.repository.Artifact;
024    import org.apache.geronimo.system.configuration.GBeanOverride;
025    
026    /**
027     * Various metadata on a configuration that's used when listing, importing,
028     * and exporting configurations.
029     *
030     * @version $Rev: 565907 $ $Date: 2007-08-14 16:51:49 -0400 (Tue, 14 Aug 2007) $
031     */
032    public class PluginMetadata implements Serializable, Comparable {
033        private final String name;
034        private final Artifact moduleId;
035        private final String category;
036        private final String description;
037        private final String pluginURL;
038        private final String author;
039        private License[] licenses = new License[0];
040        private final Hash hash;
041        private geronimoVersions[] geronimoVersions = new geronimoVersions[0];
042        private String[] jvmVersions = new String[0];
043        private Prerequisite[] prerequisites = new Prerequisite[0];
044        private String[] dependencies = new String[0];
045        private String[] forceStart = new String[0];
046        private String[] obsoletes = new String[0];
047        private URL[] repositories = new URL[0];
048        private CopyFile[] filesToCopy = new CopyFile[0];
049        private GBeanOverride[] configXmls = new GBeanOverride[0];
050    
051        private final boolean installed;
052        private final boolean eligible;
053    
054        public PluginMetadata(String name, Artifact moduleId, String category, String description, String pluginURL, String author, Hash hash, boolean installed, boolean eligible) {
055            this.name = name;
056            this.moduleId = moduleId;
057            this.category = category;
058            this.description = description;
059            this.pluginURL = pluginURL;
060            this.author = author;
061            this.hash = hash;
062            this.installed = installed;
063            this.eligible = eligible;
064        }
065    
066        public void setDependencies(String[] dependencies) {
067            this.dependencies = dependencies;
068        }
069    
070        public void setObsoletes(String[] obsoletes) {
071            this.obsoletes = obsoletes;
072        }
073    
074        public void setForceStart(String[] forceStart) {
075            this.forceStart = forceStart;
076        }
077    
078        /**
079         * Gets the Config ID for this configuration, which is a globally unique
080         * identifier.
081         */
082        public Artifact getModuleId() {
083            return moduleId;
084        }
085    
086        /**
087         * Gets a human-readable name for this configuration.
088         */
089        public String getName() {
090            return name;
091        }
092    
093        /**
094         * Gets a description of this configuration and why it is interesting
095         */
096        public String getDescription() {
097            return description;
098        }
099    
100        /**
101         * Gets a description of this module in HTML format (with paragraph
102         * markers).
103         */
104        public String getHTMLDescription() {
105            String[] paras = splitParas(description);
106            StringBuffer buf = new StringBuffer();
107            for (int i = 0; i < paras.length; i++) {
108                String para = paras[i];
109                buf.append("<p>").append(para).append("</p>\n");
110            }
111            return buf.toString();
112        }
113    
114        /**
115         * Gets a category name for this configuration.  In a list, configurations
116         * in the same category will be listed together.  There are no specific
117         * allowed values, though each repository may have standards for that.
118         */
119        public String getCategory() {
120            return category;
121        }
122    
123        public boolean isInstalled() {
124            return installed;
125        }
126    
127        public String getVersion() {
128            return moduleId.getVersion() == null ? "unknown version" : moduleId.getVersion().toString();
129        }
130    
131        /**
132         * Gets the JAR or configuration dependencies for this configuration,  Each
133         * String in the result is an Artifact (or Config ID) in String form.
134         * Generally speaking, the dependency names may be partial artifact names
135         * (but not, for example, if this whole thing is a plugin list).
136         */
137        public String[] getDependencies() {
138            return dependencies;
139        }
140    
141        /**
142         * Gets the configurations obsoleted by this configuration.  Each
143         * String in the result is an Artifact (or Config ID) in String form.
144         */
145        public String[] getObsoletes() {
146            return obsoletes;
147        }
148    
149        /**
150         * Gets the configurations that should definitely be started when the
151         * install process completes.
152         */
153        public String[] getForceStart() {
154            return forceStart;
155        }
156    
157        public geronimoVersions[] getGeronimoVersions() {
158            return geronimoVersions;
159        }
160    
161        public String getAuthor() {
162            return author;
163        }
164    
165        public Hash getHash() {
166            return hash;
167        }
168    
169        public String getPluginURL() {
170            return pluginURL;
171        }
172    
173        public URL[] getRepositories() {
174            return repositories;
175        }
176    
177        public void setGeronimoVersions(geronimoVersions[] geronimoVersions) {
178            this.geronimoVersions = geronimoVersions;
179        }
180    
181        public License[] getLicenses() {
182            return licenses;
183        }
184    
185        public void setLicenses(License[] licenses) {
186            this.licenses = licenses;
187        }
188    
189        public String[] getJvmVersions() {
190            return jvmVersions;
191        }
192    
193        public void setJvmVersions(String[] jdkVersions) {
194            this.jvmVersions = jdkVersions;
195        }
196    
197        public Prerequisite[] getPrerequisites() {
198            return prerequisites;
199        }
200    
201        public void setRepositories(URL[] repositories) {
202            this.repositories = repositories;
203        }
204    
205        public void setPrerequisites(Prerequisite[] prerequisites) {
206            this.prerequisites = prerequisites;
207        }
208    
209        public boolean isEligible() {
210            return eligible;
211        }
212    
213        /**
214         * Gets a list of files to copy from the plugin CAR into the server installation.
215         */
216        public CopyFile[] getFilesToCopy() {
217            return filesToCopy;
218        }
219    
220        public void setFilesToCopy(CopyFile[] filesToCopy) {
221            this.filesToCopy = filesToCopy;
222        }
223    
224        /**
225         * Gets a list of settings to populate in config.xml
226         */
227        public GBeanOverride[] getConfigXmls() {
228            return configXmls;
229        }
230    
231        public void setConfigXmls(GBeanOverride[] configXmls) {
232            this.configXmls = configXmls;
233        }
234    
235        public int compareTo(Object o) {
236            PluginMetadata other = (PluginMetadata) o;
237            int test = category.compareTo(other.category);
238            if(test != 0) return test;
239            test = name.compareTo(other.name);
240    
241            return test;
242        }
243    
244        public static class License implements Serializable {
245            private final String name;
246            private final boolean osiApproved;
247    
248            public License(String name, boolean osiApproved) {
249                this.name = name;
250                this.osiApproved = osiApproved;
251            }
252    
253            public String getName() {
254                return name;
255            }
256    
257            public boolean isOsiApproved() {
258                return osiApproved;
259            }
260        }
261    
262        public static class Hash implements Serializable {
263            private final String type; // MD5 or SHA-1
264            private final String value;
265    
266            public Hash(String type, String value) {
267                this.type = type;
268                this.value = value;
269            }
270    
271            public String getType() {
272                return type;
273            }
274    
275            public String getValue() {
276                return value;
277            }
278        }
279    
280        public static class CopyFile implements Serializable {
281            private final boolean relativeToServer;  // if not, relative to the Geronimo install directory
282            private final String sourceFile;
283            private final String destDir;
284    
285            public CopyFile(boolean relativeToServer, String sourceFile, String destDir) {
286                this.relativeToServer = relativeToServer;
287                this.sourceFile = sourceFile;
288                this.destDir = destDir;
289            }
290    
291            public boolean isRelativeToServer() {
292                return relativeToServer;
293            }
294    
295            public String getSourceFile() {
296                return sourceFile;
297            }
298    
299            public String getDestDir() {
300                return destDir;
301            }
302        }
303    
304        public static class Prerequisite implements Serializable {
305            private final Artifact moduleId;
306            private final String resourceType;
307            private final String description;
308            private final boolean present;
309    
310            public Prerequisite(Artifact moduleId, boolean present) {
311                this.moduleId = moduleId;
312                this.present = present;
313                resourceType = null;
314                description = null;
315            }
316    
317            public Prerequisite(Artifact moduleId, boolean present, String resourceType, String description) {
318                this.moduleId = moduleId;
319                this.present = present;
320                this.resourceType = resourceType;
321                this.description = description;
322            }
323    
324            public Artifact getModuleId() {
325                return moduleId;
326            }
327    
328            public String getResourceType() {
329                return resourceType;
330            }
331    
332            public String getDescription() {
333                return description;
334            }
335    
336            public boolean isPresent() {
337                return present;
338            }
339    
340            public String getModuleIdWithStars() {
341                StringBuffer buf = new StringBuffer();
342                if(moduleId.getGroupId() == null) {
343                    buf.append("*");
344                } else {
345                    buf.append(moduleId.getGroupId());
346                }
347                buf.append("/");
348                if(moduleId.getArtifactId() == null) {
349                    buf.append("*");
350                } else {
351                    buf.append(moduleId.getArtifactId());
352                }
353                buf.append("/");
354                if(moduleId.getVersion() == null) {
355                    buf.append("*");
356                } else {
357                    buf.append(moduleId.getVersion());
358                }
359                buf.append("/");
360                if(moduleId.getType() == null) {
361                    buf.append("*");
362                } else {
363                    buf.append(moduleId.getType());
364                }
365                return buf.toString();
366            }
367        }
368    
369        private static String[] splitParas(String desc) {
370            int start = 0, last=0;
371            List list = new ArrayList();
372            boolean inSpace = false, multiple = false;
373            for(int i=0; i<desc.length(); i++) {
374                char c = desc.charAt(i);
375                if(inSpace) {
376                    if(Character.isWhitespace(c)) {
377                        if(c == '\r' || c == '\n') {
378                            multiple = true;
379                            for(int j=i+1; j<desc.length(); j++) {
380                                char d = desc.charAt(j);
381                                if(d != c && (d == '\r' || d == '\n')) {
382                                    i = j;
383                                } else {
384                                    break;
385                                }
386                            }
387                        }
388                    } else {
389                        if(multiple) {
390                            list.add(desc.substring(last, start).trim());
391                            last = i;
392                        }
393                        inSpace = false;
394                    }
395                } else {
396                    if(c == '\r' || c == '\n') {
397                        inSpace = true;
398                        multiple = false;
399                        start = i;
400                        for(int j=i+1; j<desc.length(); j++) {
401                            char d = desc.charAt(j);
402                            if(d != c && (d == '\r' || d == '\n')) {
403                                i = j;
404                            } else {
405                                break;
406                            }
407                        }
408                    }
409                }
410            }
411            if(last < desc.length()) {
412                list.add(desc.substring(last).trim());
413            }
414            return (String[]) list.toArray(new String[list.size()]);
415        }
416    
417        public static class geronimoVersions implements Serializable {
418            
419            private String version;
420            private String moduleId;
421            private String[] repository;
422            private Prerequisite[] preReqs;
423            
424            public geronimoVersions( String version, String moduleId, String[] repository, Prerequisite[] preReqs) {
425                            this.version = version;
426                            this.moduleId = moduleId;
427                            this.repository = repository;
428                            this.preReqs = preReqs;
429            }
430            
431            public String getVersion() {
432                    return version;
433            }
434            
435            public void setVersion(String ver) {
436                    version = ver;
437            }
438            
439            public String getModuleId() {
440                    return moduleId;
441            }
442            
443            public String[] getRepository() {
444                    return repository;
445            }
446            
447            public Prerequisite[] getPreReqs() {
448                    return preReqs;
449            }
450            
451            public void setPreReqs(Prerequisite[] prereqs) {
452                    preReqs = prereqs;
453            }
454        }
455    }