KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > examples > setters > SetterService


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.examples.setters;
16
17 import java.io.IOException JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.apache.examples.Adder;
23 import org.apache.hivemind.Resource;
24
25 /**
26  * Demonstrates the use of setter based dependency injection using the BuilderFactory.
27  *
28  * @author Achim Huegen
29  */

30 public class SetterService implements Runnable JavaDoc
31 {
32     private String JavaDoc _stringValue;
33
34     private int _intValue;
35
36     private double _doubleValue;
37
38     private Adder _adderService;
39
40     private List JavaDoc _configuration;
41
42     private Collection JavaDoc _container;
43
44     private Resource _textResource;
45
46     public void setAdderService(Adder adderService)
47     {
48         _adderService = adderService;
49     }
50
51     public void setConfiguration(List JavaDoc configuration)
52     {
53         _configuration = configuration;
54     }
55
56     public void setContainer(Collection JavaDoc container)
57     {
58         _container = container;
59     }
60
61     public void setDoubleValue(double doubleValue)
62     {
63         _doubleValue = doubleValue;
64     }
65
66     public void setIntValue(int intValue)
67     {
68         _intValue = intValue;
69     }
70
71     public void setStringValue(String JavaDoc stringValue)
72     {
73         _stringValue = stringValue;
74     }
75
76     public void setTextResource(Resource textResource)
77     {
78         _textResource = textResource;
79     }
80
81     public void run()
82     {
83         System.out.println("StringValue: " + _stringValue);
84         System.out.println("IntValue: " + _intValue);
85         System.out.println("DoubleValue: " + _doubleValue);
86         System.out.println("AdderService result: " + _adderService.add(10, 20));
87         System.out.println("Configuration size: " + _configuration.size());
88         System.out.println("Container type: " + _container.getClass().getName());
89         Properties JavaDoc properties = loadResource();
90         System.out.println("Text resource content: " + properties.toString());
91     }
92
93     /**
94      * Loads the properties file reference by property textResource
95      */

96     private Properties JavaDoc loadResource()
97     {
98         Properties JavaDoc properties = new Properties JavaDoc();
99         try
100         {
101             properties.load(_textResource.getResourceURL().openStream());
102         }
103         catch (IOException JavaDoc ignore)
104         {
105         }
106         return properties;
107     }
108 }
109
Popular Tags