KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > portlets > admin > DaemonAdminPortlet


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.portal.portlets.admin;
18
19 //Element Construction Set
20
import org.apache.ecs.html.B;
21 import org.apache.ecs.html.Form;
22 import org.apache.ecs.html.Input;
23 import org.apache.ecs.html.P;
24 import org.apache.ecs.html.Table;
25 import org.apache.ecs.html.TD;
26 import org.apache.ecs.html.TH;
27 import org.apache.ecs.html.TR;
28 import org.apache.ecs.ConcreteElement;
29 import org.apache.ecs.ElementContainer;
30 import org.apache.ecs.StringElement;
31
32 //Jetspeed stuff
33
import org.apache.jetspeed.portal.portlets.AbstractPortlet;
34 import org.apache.jetspeed.portal.PortletException;
35 import org.apache.jetspeed.daemon.Daemon;
36 import org.apache.jetspeed.daemon.DaemonEntry;
37 import org.apache.jetspeed.daemon.DaemonNotFoundException;
38 import org.apache.jetspeed.daemon.impl.FeedDaemon;
39 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
40 import org.apache.jetspeed.services.logging.JetspeedLogger;
41 import org.apache.jetspeed.services.daemonfactory.DaemonFactory;
42
43 //turbine
44
import org.apache.turbine.util.ParameterParser;
45 import org.apache.turbine.util.RunData;
46
47 //standard java stuff
48
import java.util.*;
49
50 /**
51 Handles enumerating Portlets that are also applications
52
53 @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
54 @author <a HREF="mailto:raphael@apache.org">Raphael Luta</a>
55 @author <a HREF="mailto:sgala@apache.org">Santiago Gala</a>
56 @version $Id: DaemonAdminPortlet.java,v 1.30 2004/02/23 03:26:19 jford Exp $
57 */

58 public class DaemonAdminPortlet extends AbstractPortlet
59 {
60
61     /**
62      * Static initialization of the logger for this class
63      */

64     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DaemonAdminPortlet.class.getName());
65     
66     public static final String JavaDoc REFRESH = "Start";
67
68     /**
69     Key for starting daemon manually from a form
70     */

71     private static final String JavaDoc DAEMON = "daemon";
72
73
74     public ConcreteElement getContent( RunData rundata ) {
75         
76         try {
77
78             if ( rundata.getParameters().getString( REFRESH ) != null ) {
79
80                 String JavaDoc daemon = rundata.getParameters().getString( DAEMON );
81
82                 try {
83                     rundata.getParameters().remove( REFRESH );
84                     rundata.getParameters().remove( DAEMON );
85
86                     DaemonEntry entry = DaemonFactory.getDaemonEntry( daemon );
87
88
89                     logger.info( "Admin -> asking " + daemon + " to run..." );
90                     //now that we have the DaemonEntry kick it off for processing
91
DaemonFactory.process( entry );
92
93                 } catch (DaemonNotFoundException e) {
94                     logger.error( "Could not process daemon...", e );
95                 }
96             }
97
98
99             Table table = new Table().setWidth("100%");
100
101             table.addElement( new TR().addElement( new TH() )
102                                       .addElement( new TH("Name") )
103                                       .addElement( new TH("Status") )
104                                       .addElement( new TH("Result") )
105                                       .addElement( new TH("On Startup") )
106                                       .addElement( new TH("Interval") )
107                                       .addElement( new TH("Classname") ) );
108
109             DaemonEntry[] entries = DaemonFactory.getDaemonEntries();
110
111             for (int i = 0; i < entries.length; ++i) {
112
113                 table.addElement( new TR()
114                                         .addElement( new TD( this.getForm( entries[i].getName(), rundata ) ) )
115                                         .addElement( new TD( entries[i].getName() ) )
116                                         .addElement( new TD( this.getStatus( DaemonFactory.getStatus( entries[i] ) ) ) )
117                                         .addElement( new TD( this.getResult( DaemonFactory.getResult( entries[i] ) ) ) )
118                                         .addElement( new TD( new Boolean JavaDoc( entries[i].onStartup() ).toString() ) )
119                                         .addElement( new TD( Long.toString( entries[i].getInterval() ) ) )
120                                         .addElement( new TD( entries[i].getClassname() ) ) );
121
122                 String JavaDoc message = DaemonFactory.getMessage( entries[i] );
123
124                 if ( message != null ) {
125
126                     message = entries[i].getName() + ": " + message;
127
128                     table.addElement( new TR().addElement( new TD().setColSpan( 7 )
129                                 .addElement( message ) ) );
130
131                 }
132
133             }
134
135             ElementContainer content = new ElementContainer();
136
137             //content.addElement( this.getStatus() );
138
//content.addElement( this.getFeedCount() );
139
content.addElement( table );
140             //content.addElement( this.getForm() );
141

142             return content;
143
144         } catch ( Throwable JavaDoc t ) {
145             logger.error("Throwable", t);
146             return new StringElement( t.getMessage() );
147         }
148
149     }
150
151
152     /**
153     @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
154     */

155     private String JavaDoc getStatus(int status) {
156
157         String JavaDoc message = "Unknown";
158
159         switch(status) {
160             case Daemon.STATUS_NOT_PROCESSED:
161                 message = "Not processed";
162             break;
163
164             case Daemon.STATUS_PROCESSING:
165                 message = "Processing...";
166             break;
167             case Daemon.STATUS_PROCESSED:
168                 message = "Processed";
169             break;
170
171         }
172
173         return message;
174
175     }
176
177     /**
178     @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
179     */

180     private String JavaDoc getResult( int result ) {
181
182         String JavaDoc message = "Unknown";
183
184         switch (result) {
185             case Daemon.RESULT_SUCCESS:
186                 message = "Success";
187             break;
188
189             case Daemon.RESULT_FAILED:
190                 message = "Failed";
191             break;
192
193             case Daemon.RESULT_PROCESSING:
194                 message = "Processing...";
195             break;
196         }
197
198         return message;
199
200     }
201
202
203     /**
204     @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
205     */

206     private ConcreteElement getFeedCount() {
207
208         return new P().addElement( new B( "Current number of feeds: ") )
209                       .addElement( Integer.toString( FeedDaemon.getCount() ) );
210
211     }
212
213
214     /**
215     Return a form that can refresh the current daemon
216
217     @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
218     */

219     private Form getForm( String JavaDoc daemon, RunData rundata ) {
220
221
222         Form form = new Form();
223
224         ParameterParser params = rundata.getParameters();
225         Enumeration keys = params.keys();
226         while( keys.hasMoreElements() ) {
227             String JavaDoc key = (String JavaDoc)keys.nextElement();
228             String JavaDoc value = (String JavaDoc)params.getString(key, "");
229             form.addElement( new Input( ).setName( key )
230                 .setType( "hidden" )
231                 .setValue( value ) );
232         }
233
234
235
236         form.addElement( new Input().setType( "submit" )
237                                     .setName( REFRESH )
238                                     .setValue( REFRESH ) );
239
240         form.addElement( new Input().setType( "hidden" )
241                                     .setName( DAEMON )
242                                     .setValue( daemon ) );
243
244         return form;
245     }
246
247
248     /**
249     @author <a HREF="mailto:burton@apache.org">Kevin A. Burton</a>
250     */

251     public void init() throws PortletException {
252
253         this.setTitle("Daemons");
254
255         this.setDescription("Configure your daemon...");
256
257     }
258
259     public boolean getAllowEdit( RunData rundata ) {
260         return false;
261     }
262
263     public boolean getAllowMaximize(RunData rundata ) {
264         return false;
265     }
266
267
268 }
269
Popular Tags