001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.geronimo.commands;
021
022 import java.io.File;
023 import java.io.PrintWriter;
024 import java.io.StringWriter;
025 import java.io.IOException;
026 import java.io.InputStream;
027 import java.util.Properties;
028
029 import org.apache.geronimo.gshell.ansi.Buffer;
030 import org.apache.geronimo.gshell.ansi.Code;
031 import org.apache.geronimo.gshell.ansi.RenderWriter;
032 import org.apache.geronimo.gshell.branding.Branding;
033 import org.apache.geronimo.gshell.branding.BrandingSupport;
034 import org.apache.geronimo.gshell.branding.VersionLoader;
035 import org.codehaus.plexus.component.annotations.Component;
036 import org.codehaus.plexus.component.annotations.Requirement;
037 import org.codehaus.plexus.util.StringUtils;
038 import org.codehaus.plexus.util.IOUtil;
039
040 import jline.Terminal;
041
042 /**
043 * Provides the branding for Geronimo usage of GShell.
044 *
045 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
046 */
047 @Component(role=Branding.class, hint="geronimo")
048 public class GeronimoBranding
049 extends BrandingSupport
050 {
051 //
052 // FIXME: This needs work before it can really be used...
053 //
054
055 // @Requirement
056 // private VersionLoader versionLoader;
057
058 @Requirement
059 private Terminal terminal;
060
061 //
062 // FIXME: Don't override this... leave things as 'gshell' until we have more use-cases for branding fluff
063 //
064
065 public String getName() {
066 return "gshell";
067 }
068
069 public String getDisplayName() {
070 return "Apache Geronimo";
071 }
072
073 public String getProgramName() {
074 //
075 // FIXME: For now we leave this as 'gsh' ...
076 //
077 return System.getProperty("program.name", "gsh");
078 }
079
080 public String getAbout() {
081 StringWriter writer = new StringWriter();
082 PrintWriter out = new RenderWriter(writer);
083
084 out.println("For information about @|cyan " + getDisplayName() + "|, visit:");
085 out.println(" @|bold http://geronimo.apache.org| ");
086 out.flush();
087
088 return writer.toString();
089 }
090
091 //
092 // HACK: Just duplicating the PropertiesVersionLoader here for now
093 //
094
095 private Properties props;
096
097 public String getVersion() {
098 if (props == null) {
099 String resourceName = "version.properties";
100 InputStream input = getClass().getResourceAsStream(resourceName);
101 assert input != null;
102
103 try {
104 props = new Properties();
105 props.load(input);
106 }
107 catch (IOException e) {
108 throw new RuntimeException("Failed to load: " + resourceName, e);
109 }
110 finally {
111 IOUtil.close(input);
112 }
113 }
114
115 return props.getProperty("version");
116 }
117
118 public String getWelcomeBanner() {
119 StringWriter writer = new StringWriter();
120 PrintWriter out = new RenderWriter(writer);
121
122 out.println("@|bold " + getDisplayName() + "| (" + getVersion() + ")");
123 out.println();
124 out.println("Type '@|bold help|' for more information.");
125
126 // If we can't tell, or have something bogus then use a reasonable default
127 int width = terminal.getTerminalWidth();
128 if (width < 1) {
129 width = 80;
130 }
131
132 out.print(StringUtils.repeat("-", width - 1));
133
134 out.flush();
135
136 return writer.toString();
137 }
138 }