Source Code Cross Referenced for ComponentTemplateSourceImpl.java in  » Library » Tapestry » org » apache » tapestry » internal » services » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Tapestry » org.apache.tapestry.internal.services 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright 2006, 2007 The Apache Software Foundation
002:        //
003:        // Licensed under the Apache License, Version 2.0 (the "License");
004:        // you may not use this file except in compliance with the License.
005:        // You may obtain a copy of the License at
006:        //
007:        //     http://www.apache.org/licenses/LICENSE-2.0
008:        //
009:        // Unless required by applicable law or agreed to in writing, software
010:        // distributed under the License is distributed on an "AS IS" BASIS,
011:        // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:        // See the License for the specific language governing permissions and
013:        // limitations under the License.
014:
015:        package org.apache.tapestry.internal.services;
016:
017:        import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newConcurrentMap;
018:
019:        import java.net.URL;
020:        import java.util.List;
021:        import java.util.Locale;
022:        import java.util.Map;
023:        import java.util.Set;
024:
025:        import org.apache.tapestry.internal.InternalConstants;
026:        import org.apache.tapestry.internal.event.InvalidationEventHubImpl;
027:        import org.apache.tapestry.internal.events.UpdateListener;
028:        import org.apache.tapestry.internal.parser.ComponentTemplate;
029:        import org.apache.tapestry.internal.parser.TemplateToken;
030:        import org.apache.tapestry.internal.util.MultiKey;
031:        import org.apache.tapestry.internal.util.URLChangeTracker;
032:        import org.apache.tapestry.ioc.Resource;
033:        import org.apache.tapestry.model.ComponentModel;
034:
035:        /**
036:         * Service implementation that manages a cache of parsed component templates.
037:         */
038:        public final class ComponentTemplateSourceImpl extends
039:                InvalidationEventHubImpl implements  ComponentTemplateSource,
040:                UpdateListener {
041:
042:            private final TemplateParser _parser;
043:
044:            private final PageTemplateLocator _locator;
045:
046:            private final URLChangeTracker _tracker;
047:
048:            /**
049:             * Caches from a key (combining component name and locale) to a resource. Often, many different
050:             * keys will point to the same resource (i.e., "foo:en_US", "foo:en_UK", and "foo:en" may all be
051:             * parsed from the same "foo.html" resource). The resource may end up being null, meaning the
052:             * template does not exist in any locale.
053:             */
054:            private final Map<MultiKey, Resource> _templateResources = newConcurrentMap();
055:
056:            /**
057:             * Cache of parsed templates, keyed on resource.
058:             */
059:            private final Map<Resource, ComponentTemplate> _templates = newConcurrentMap();
060:
061:            private final ComponentTemplate _missingTemplate = new ComponentTemplate() {
062:                public Set<String> getComponentIds() {
063:                    return null;
064:                }
065:
066:                public Resource getResource() {
067:                    return null;
068:                }
069:
070:                public List<TemplateToken> getTokens() {
071:                    return null;
072:                }
073:
074:                public boolean isMissing() {
075:                    return true;
076:                }
077:            };
078:
079:            public ComponentTemplateSourceImpl(TemplateParser parser,
080:                    PageTemplateLocator locator) {
081:                this (parser, locator, new URLChangeTracker());
082:            }
083:
084:            ComponentTemplateSourceImpl(TemplateParser parser,
085:                    PageTemplateLocator locator, URLChangeTracker tracker) {
086:                _parser = parser;
087:                _locator = locator;
088:                _tracker = tracker;
089:            }
090:
091:            /**
092:             * Resolves the component name to a {@link Resource} and finds the localization of that resource
093:             * (the combination of component name and locale is resolved to a resource). The localized
094:             * resource is used as the key to a cache of {@link ComponentTemplate}s.
095:             * <p>
096:             * If a template doesn't exist, then the missing ComponentTemplate is returned.
097:             */
098:            public ComponentTemplate getTemplate(ComponentModel componentModel,
099:                    Locale locale) {
100:                String componentName = componentModel.getComponentClassName();
101:
102:                MultiKey key = new MultiKey(componentName, locale);
103:
104:                // First cache is key to resource.
105:
106:                Resource resource = _templateResources.get(key);
107:
108:                if (resource == null) {
109:                    resource = locateTemplateResource(componentModel, locale);
110:                    _templateResources.put(key, resource);
111:                }
112:
113:                // If we haven't yet parsed the template into the cache, do so now.
114:
115:                ComponentTemplate result = _templates.get(resource);
116:
117:                if (result == null) {
118:                    result = parseTemplate(resource);
119:                    _templates.put(resource, result);
120:                }
121:
122:                return result;
123:            }
124:
125:            private ComponentTemplate parseTemplate(Resource r) {
126:                // In a race condition, we may parse the same template more than once. This will likely add
127:                // the resource to the tracker multiple times. Not likely this will cause a big issue.
128:
129:                URL resourceURL = r.toURL();
130:
131:                if (resourceURL == null)
132:                    return _missingTemplate;
133:
134:                _tracker.add(resourceURL);
135:
136:                return _parser.parseTemplate(r);
137:            }
138:
139:            private Resource locateTemplateResource(
140:                    ComponentModel initialModel, Locale locale) {
141:                ComponentModel model = initialModel;
142:                while (model != null) {
143:
144:                    Resource baseResource = baseResourceForModel(model);
145:                    Resource localized = baseResource.forLocale(locale);
146:
147:                    // In a race condition, we may hit this method a couple of times, and overwrite previous
148:                    // results with identical new results.
149:
150:                    // If found a properly localized version of the base resource for the model,
151:                    // then we've found a match (even if we had to ascend a couple of levels
152:                    // to reach it).
153:
154:                    if (localized != null)
155:                        return localized;
156:
157:                    // Not on the classpath, the the locator see if its a) a page and b) a resource inside
158:                    // the context
159:
160:                    localized = _locator
161:                            .findPageTemplateResource(model, locale);
162:
163:                    if (localized != null)
164:                        return localized;
165:
166:                    // Otherwise, this component doesn't have its own template ... lets work up to its
167:                    // base class and check there.
168:
169:                    model = model.getParentModel();
170:                }
171:
172:                // This will be a Resource whose URL is null, which will be picked up later and force the
173:                // return of the empty template.
174:
175:                return baseResourceForModel(initialModel);
176:            }
177:
178:            private Resource baseResourceForModel(ComponentModel model) {
179:                return model.getBaseResource().withExtension(
180:                        InternalConstants.TEMPLATE_EXTENSION);
181:            }
182:
183:            /**
184:             * Checks to see if any parsed resource has changed. If so, then all internal caches are
185:             * cleared, and an invalidation event is fired. This is brute force ... a more targetted
186:             * dependency management strategy may come later.
187:             */
188:            public void checkForUpdates() {
189:                if (_tracker.containsChanges()) {
190:                    _tracker.clear();
191:                    _templateResources.clear();
192:                    _templates.clear();
193:                    fireInvalidationEvent();
194:                }
195:            }
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.