01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
18:
19: import java.util.Locale;
20: import java.util.Map;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.tapestry.ioc.Messages;
24: import org.apache.tapestry.ioc.ObjectLocator;
25: import org.apache.tapestry.model.MutableComponentModel;
26: import org.apache.tapestry.services.ClassTransformation;
27: import org.apache.tapestry.services.InjectionProvider;
28:
29: /**
30: * Allows for a number of anonymous injections based on the type of field that is to be injected.
31: */
32: public class CommonResourcesInjectionProvider implements
33: InjectionProvider {
34: private static final Map<String, String> _configuration = newMap();
35:
36: public CommonResourcesInjectionProvider() {
37: add(Messages.class, "getMessages");
38: add(Locale.class, "getLocale");
39: add(Log.class, "getLog");
40: add(String.class, "getCompleteId");
41: }
42:
43: private void add(Class fieldType, String methodName) {
44: _configuration.put(fieldType.getName(), methodName);
45: }
46:
47: public boolean provideInjection(String fieldName, String fieldType,
48: ObjectLocator locator, ClassTransformation transformation,
49: MutableComponentModel componentModel) {
50: String implementationMethodName = _configuration.get(fieldType);
51:
52: if (implementationMethodName == null)
53: return false;
54:
55: String resourcesField = transformation.getResourcesFieldName();
56:
57: String body = String.format("%s = %s.%s();", fieldName,
58: resourcesField, implementationMethodName);
59:
60: transformation.makeReadOnly(fieldName);
61:
62: transformation.extendConstructor(body);
63:
64: return true;
65: }
66:
67: }
|