001: // Copyright 2005 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.examples.setters;
016:
017: import java.io.IOException;
018: import java.util.Collection;
019: import java.util.List;
020: import java.util.Properties;
021:
022: import org.apache.examples.Adder;
023: import org.apache.hivemind.Resource;
024:
025: /**
026: * Demonstrates the use of setter based dependency injection using the BuilderFactory.
027: *
028: * @author Achim Huegen
029: */
030: public class SetterService implements Runnable {
031: private String _stringValue;
032:
033: private int _intValue;
034:
035: private double _doubleValue;
036:
037: private Adder _adderService;
038:
039: private List _configuration;
040:
041: private Collection _container;
042:
043: private Resource _textResource;
044:
045: public void setAdderService(Adder adderService) {
046: _adderService = adderService;
047: }
048:
049: public void setConfiguration(List configuration) {
050: _configuration = configuration;
051: }
052:
053: public void setContainer(Collection container) {
054: _container = container;
055: }
056:
057: public void setDoubleValue(double doubleValue) {
058: _doubleValue = doubleValue;
059: }
060:
061: public void setIntValue(int intValue) {
062: _intValue = intValue;
063: }
064:
065: public void setStringValue(String stringValue) {
066: _stringValue = stringValue;
067: }
068:
069: public void setTextResource(Resource textResource) {
070: _textResource = textResource;
071: }
072:
073: public void run() {
074: System.out.println("StringValue: " + _stringValue);
075: System.out.println("IntValue: " + _intValue);
076: System.out.println("DoubleValue: " + _doubleValue);
077: System.out.println("AdderService result: "
078: + _adderService.add(10, 20));
079: System.out.println("Configuration size: "
080: + _configuration.size());
081: System.out.println("Container type: "
082: + _container.getClass().getName());
083: Properties properties = loadResource();
084: System.out.println("Text resource content: "
085: + properties.toString());
086: }
087:
088: /**
089: * Loads the properties file reference by property textResource
090: */
091: private Properties loadResource() {
092: Properties properties = new Properties();
093: try {
094: properties
095: .load(_textResource.getResourceURL().openStream());
096: } catch (IOException ignore) {
097: }
098: return properties;
099: }
100: }
|