KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > GenericPortletWindow


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49 package com.caucho.portal.generic;
50
51 import javax.portlet.Portlet;
52 import javax.portlet.PortletContext;
53 import javax.portlet.PortletException;
54 import java.io.IOException JavaDoc;
55 import java.util.logging.Level JavaDoc;
56 import java.util.logging.Logger JavaDoc;
57
58 /**
59  * A Window that contains one {@link javax.portlet.Portlet}.
60  */

61 public class GenericPortletWindow
62   extends GenericWindow
63 {
64   static protected final Logger JavaDoc log =
65     Logger.getLogger(GenericPortletWindow.class.getName());
66
67   private Portlet _portlet;
68   private boolean _isPortletNameSet;
69
70   public GenericPortletWindow()
71   {
72   }
73  
74   /**
75    * The portlet. This method can be called in derived classes,
76    * or through the use of dependency injection on containers that support it.
77    */

78   public void setPortlet(Portlet portlet)
79   {
80     if (_portlet != null)
81       throw new IllegalArgumentException JavaDoc("`portlet' is already set");
82
83     _portlet = portlet;
84   }
85
86
87   /**
88    * An alternative to {@link #setPortlet(Portlet)}, specify the class
89    * name of an object to instantiate
90    */

91   public void setPortletClass(String JavaDoc className)
92   {
93     setPortlet( (Portlet) newInstance(Portlet.class, className) );
94   }
95
96   /**
97    * The default is the value of portlet-class.
98    */

99   public void setPortletName(String JavaDoc portletName)
100   {
101     super.setPortletName(portletName);
102     _isPortletNameSet = true;
103   }
104
105   public void init(PortletContext portletContext)
106     throws PortletException
107   {
108     super.init(portletContext);
109
110     if (_portlet == null)
111       throw new PortletException("`portlet' is required");
112
113     if (!_isPortletNameSet)
114        setPortletName(_portlet.getClass().getName());
115
116     _portlet.init(this);
117   }
118
119
120   protected Portlet getPortlet()
121   {
122     if (_portlet == null)
123       throw new IllegalStateException JavaDoc("missing init()?");
124
125     return _portlet;
126   }
127
128   /**
129    * Use the PortletConnection to get an Action appropriate for this window,
130    * and then call processAction on the portlet that is contained within this
131    * Window if the Portlet is the target of the action.
132    */

133   public void processAction(PortletConnection connection)
134     throws PortletException, IOException JavaDoc
135   {
136     Action action = connection.getAction(this, getNamespace());
137
138     if (action != null) {
139       try {
140         if (action.isTarget())
141           action.processAction(getPortlet());
142       }
143       finally {
144         action.finish();
145       }
146     }
147   }
148
149   /**
150    * Use the PortletConnection to get a Render appropriate for this window,
151    * and then call render on the portlet that is contained within this
152    * Window.
153    */

154   public void render(PortletConnection connection)
155     throws PortletException, IOException JavaDoc
156   {
157     Render render = connection.getRender(this, getNamespace());
158
159     if (render != null) {
160       try {
161         render.render(getPortlet());
162       }
163       finally {
164         render.finish();
165       }
166     }
167   }
168
169   public void destroy()
170   {
171     Portlet portlet = _portlet;
172
173     _portlet = null;
174
175     try {
176       if (portlet != null)
177         portlet.destroy();
178     }
179     catch (Exception JavaDoc ex) {
180       log.log(Level.WARNING, ex.toString(), ex);
181     }
182   }
183 }
184
185
Popular Tags