KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > resource > DynamicResource


1 /*
2  * $Id: DynamicResource.java,v 1.5 2005/05/09 18:56:27 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.resource;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.RequestURL;
19 import org.wings.Resource;
20 import org.wings.SFrame;
21 import org.wings.SimpleURL;
22 import org.wings.externalizer.ExternalizeManager;
23 import org.wings.session.PropertyService;
24 import org.wings.session.SessionManager;
25 import org.wings.util.StringUtil;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * Dynamic Resources are web resources representing rendered components
32  * and are individually loaded by Browsers as different 'files'.
33  * Dynamic Resources include therefore frames, cascading stylesheets or
34  * script files. The externalizer gives them a uniqe name.
35  * The resources may change in the consequence of some internal change of
36  * the components. This invalidation process yields a new 'version', called
37  * epoch here. The epoch is part of the externalized name.
38  */

39 public abstract class DynamicResource
40         extends Resource {
41     private final transient static Log log = LogFactory.getLog(DynamicResource.class);
42
43     /**
44      * The epoch of this resource. With each invalidation, this counter
45      * is incremented.
46      */

47     private int epoch = 0;
48
49     // byte[] ? Since we use this to write it to a stream ..
50
/**
51      * The epoch as string representation. The epoch is converted in a string
52      * that is as short as possible. This is done once whenever the epoch
53      * changes to do this conversion only once.
54      */

55     private String JavaDoc epochCache = "W" + StringUtil.toShortestAlphaNumericString(epoch);
56
57     /**
58      * The frame, to which this resource belongs.
59      */

60     private SFrame frame;
61
62     protected DynamicResource(String JavaDoc extension, String JavaDoc mimeType) {
63         super(extension, mimeType);
64     }
65
66
67     public DynamicResource(SFrame frame) {
68         this(frame, "", "");
69     }
70
71
72     public DynamicResource(SFrame frame, String JavaDoc extension, String JavaDoc mimeType) {
73         super(extension, mimeType);
74         this.frame = frame;
75     }
76
77     /**
78      * Return the frame, to which this resource belongs.
79      */

80     public final SFrame getFrame() {
81         return frame;
82     }
83
84     public String JavaDoc getId() {
85         if (id == null) {
86             ExternalizeManager ext = SessionManager.getSession().getExternalizeManager();
87             id = ext.getId(ext.externalize(this));
88             log.debug("new " + getClass().getName() + " with id " + id);
89         }
90         return id;
91     }
92
93     /**
94      * Mark this dynamic resource as to be re-rendered. This method is
95      * called, whenever some change took place in the frame, so that this
96      * dynamic resource is to be externalized with a new version-number.
97      */

98     public final void invalidate() {
99         epochCache = "W" + StringUtil.toShortestAlphaNumericString(++epoch);
100         if (log.isDebugEnabled()) {
101             String JavaDoc name = getClass().getName();
102             name = name.substring(name.lastIndexOf(".") + 1);
103             log.debug("[" + name + "] " +
104                     "invalidate - epoch: " + epochCache);
105         }
106
107     }
108
109
110     public final String JavaDoc getEpoch() {
111         return epochCache;
112     }
113
114     public SimpleURL getURL() {
115         RequestURL requestURL = (RequestURL) getPropertyService().getProperty("request.url");
116         if (requestURL != null) {
117             requestURL = (RequestURL) requestURL.clone();
118             requestURL.setEpoch(getEpoch());
119             requestURL.setResource(getId());
120         }
121         return requestURL;
122     }
123
124     private PropertyService propertyService;
125
126     protected PropertyService getPropertyService() {
127         if (propertyService == null)
128             propertyService = (PropertyService) SessionManager.getSession();
129         return propertyService;
130     }
131
132
133     public String JavaDoc toString() {
134         return getId() + " " + getEpoch();
135     }
136
137
138     /**
139      * Get additional http-headers.
140      * Returns <tt>null</tt>, if there are no additional headers to be set.
141      *
142      * @return Set of {@link java.util.Map.Entry} (key-value pairs)
143      */

144     public Collection JavaDoc getHeaders() {
145         return null;
146     }
147
148     /**
149      * Get additional http-headers.
150      * Returns <tt>null</tt>, if there are no additional headers to be set.
151      *
152      * @return Set of {@link java.util.Map.Entry} (key-value pairs)
153      */

154     public Set JavaDoc getCookies() {
155         return null;
156     }
157 }
158
159
160
Popular Tags