View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.composite;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.ObjectInput;
25  import java.io.ObjectOutput;
26  import java.net.URL;
27  import java.util.Map;
28  
29  import javax.faces.FacesWrapper;
30  import javax.faces.application.Resource;
31  import javax.faces.context.FacesContext;
32  
33  /**
34   * The value inside composite component attribute map with the key
35   * Resource.COMPONENT_RESOURCE_KEY should be a Serializable. This
36   * wrapper add serialization to Resource instances, because 
37   * ResourceImpl depends from the ResourceLoader used by it.
38   * 
39   * @author Leonardo Uribe (latest modification by $Author$)
40   * @version $Revision$ $Date$
41   */
42  public final class CompositeResouceWrapper extends Resource 
43      implements FacesWrapper<Resource>, Externalizable
44  {
45      /**
46       * 
47       */
48      private static final long serialVersionUID = 8067930634887545843L;
49      
50      private transient Resource _delegate;
51      
52      public CompositeResouceWrapper()
53      {
54          super();
55      }
56      
57      public CompositeResouceWrapper(Resource delegate)
58      {
59          super();
60          this._delegate = delegate;
61          setResourceName(delegate.getResourceName());
62          setLibraryName(delegate.getLibraryName());
63          setContentType(delegate.getContentType());
64      }
65  
66  
67      public InputStream getInputStream() throws IOException
68      {
69          return getWrapped().getInputStream();
70      }
71  
72      public String getRequestPath()
73      {
74          return getWrapped().getRequestPath();
75      }
76  
77      public Map<String, String> getResponseHeaders()
78      {
79          return getWrapped().getResponseHeaders();
80      }
81  
82      public URL getURL()
83      {
84          return getWrapped().getURL();
85      }
86  
87      public boolean userAgentNeedsUpdate(FacesContext context)
88      {
89          return getWrapped().userAgentNeedsUpdate(context);
90      }
91  
92      public Resource getWrapped()
93      {
94          if (_delegate == null)
95          {
96              _delegate = FacesContext.getCurrentInstance().getApplication().
97                              getResourceHandler().createResource(
98                                      getResourceName(), 
99                                      getLibraryName(),
100                                     getContentType());
101         }
102         return _delegate;
103     }
104 
105     public void readExternal(ObjectInput in) throws IOException,
106             ClassNotFoundException
107     {
108         setResourceName((String) in.readObject());
109         setLibraryName((String) in.readObject());
110         setContentType((String) in.readObject());
111     }
112 
113     public void writeExternal(ObjectOutput out) throws IOException
114     {
115         out.writeObject(getResourceName());
116         out.writeObject(getLibraryName());
117         out.writeObject(getContentType());
118     }
119     
120     @Override
121     public String toString()
122     {
123         // Delegate resource in this case could not be available in serialization, or
124         // serialization could happen in other context. So it is better to return
125         // a simple String representing the object without go into delegation.
126         return ( (getLibraryName() != null) ? getLibraryName() : "") + ":"+ 
127                ( (getResourceName() != null) ? getResourceName() : "") ;
128     }
129 }