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 { 032 private String _stringValue; 033 034 private int _intValue; 035 036 private double _doubleValue; 037 038 private Adder _adderService; 039 040 private List _configuration; 041 042 private Collection _container; 043 044 private Resource _textResource; 045 046 public void setAdderService(Adder adderService) 047 { 048 _adderService = adderService; 049 } 050 051 public void setConfiguration(List configuration) 052 { 053 _configuration = configuration; 054 } 055 056 public void setContainer(Collection container) 057 { 058 _container = container; 059 } 060 061 public void setDoubleValue(double doubleValue) 062 { 063 _doubleValue = doubleValue; 064 } 065 066 public void setIntValue(int intValue) 067 { 068 _intValue = intValue; 069 } 070 071 public void setStringValue(String stringValue) 072 { 073 _stringValue = stringValue; 074 } 075 076 public void setTextResource(Resource textResource) 077 { 078 _textResource = textResource; 079 } 080 081 public void run() 082 { 083 System.out.println("StringValue: " + _stringValue); 084 System.out.println("IntValue: " + _intValue); 085 System.out.println("DoubleValue: " + _doubleValue); 086 System.out.println("AdderService result: " + _adderService.add(10, 20)); 087 System.out.println("Configuration size: " + _configuration.size()); 088 System.out.println("Container type: " + _container.getClass().getName()); 089 Properties properties = loadResource(); 090 System.out.println("Text resource content: " + properties.toString()); 091 } 092 093 /** 094 * Loads the properties file reference by property textResource 095 */ 096 private Properties loadResource() 097 { 098 Properties properties = new Properties(); 099 try 100 { 101 properties.load(_textResource.getResourceURL().openStream()); 102 } 103 catch (IOException ignore) 104 { 105 } 106 return properties; 107 } 108 }