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 java.util.Locale;
018:
019: import org.apache.tapestry.internal.events.InvalidationListener;
020: import org.apache.tapestry.internal.events.UpdateListener;
021: import org.apache.tapestry.internal.util.URLChangeTracker;
022: import org.apache.tapestry.ioc.Messages;
023: import org.apache.tapestry.ioc.Resource;
024: import org.apache.tapestry.model.ComponentModel;
025: import org.apache.tapestry.services.ComponentMessagesSource;
026:
027: public class ComponentMessagesSourceImpl implements
028: ComponentMessagesSource, UpdateListener {
029: private final MessagesSource _messagesSource;
030:
031: private final Resource _rootResource;
032:
033: private final String _appCatalog;
034:
035: private static class ComponentModelBundle implements MessagesBundle {
036: private final ComponentModel _model;
037:
038: private final MessagesBundle _rootBundle;
039:
040: public ComponentModelBundle(ComponentModel model,
041: MessagesBundle rootBundle) {
042: _model = model;
043: _rootBundle = rootBundle;
044: }
045:
046: public Resource getBaseResource() {
047: return _model.getBaseResource();
048: }
049:
050: public Object getId() {
051: return _model.getComponentClassName();
052: }
053:
054: public MessagesBundle getParent() {
055: ComponentModel parentModel = _model.getParentModel();
056:
057: if (parentModel == null)
058: return _rootBundle;
059:
060: return new ComponentModelBundle(parentModel, _rootBundle);
061: }
062: }
063:
064: public ComponentMessagesSourceImpl(Resource rootResource,
065: String appCatalog) {
066: this (rootResource, appCatalog, new URLChangeTracker());
067: }
068:
069: ComponentMessagesSourceImpl(Resource rootResource,
070: String appCatalog, URLChangeTracker tracker) {
071: _rootResource = rootResource;
072: _appCatalog = appCatalog;
073:
074: _messagesSource = new MessagesSourceImpl(tracker);
075: }
076:
077: public void checkForUpdates() {
078: _messagesSource.checkForUpdates();
079: }
080:
081: public Messages getMessages(ComponentModel componentModel,
082: Locale locale) {
083: final Resource appCatalogResource = _rootResource
084: .forFile(_appCatalog);
085:
086: // If the application catalog exists, set it up as the root, otherwise use null.
087:
088: MessagesBundle appCatalogBundle = appCatalogResource.toURL() == null ? null
089: : new MessagesBundle() {
090: public Resource getBaseResource() {
091: return appCatalogResource;
092: }
093:
094: public Object getId() {
095: return _appCatalog;
096: }
097:
098: public MessagesBundle getParent() {
099: return null;
100: }
101: };
102:
103: MessagesBundle bundle = new ComponentModelBundle(
104: componentModel, appCatalogBundle);
105:
106: return _messagesSource.getMessages(bundle, locale);
107: }
108:
109: public void addInvalidationListener(InvalidationListener listener) {
110: _messagesSource.addInvalidationListener(listener);
111: }
112: }
|