KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > gs > filters > JavaSpaceTemplateFilter


1 /*
2  * $Id: JavaSpaceTemplateFilter.java 3807 2006-11-06 13:13:36Z holger $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.gs.filters;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import net.jini.core.entry.Entry;
19
20 import org.apache.commons.beanutils.BeanUtils;
21 import org.apache.commons.lang.StringUtils;
22 import org.mule.providers.gs.JiniMessage;
23 import org.mule.umo.UMOMessage;
24 import org.mule.util.ClassUtils;
25
26 /**
27  * Configures an entry template on a JavaSpace endpoint.
28  */

29 public class JavaSpaceTemplateFilter implements JavaSpaceFilter
30 {
31     public static final String JavaDoc NULL_VALUE = "null";
32
33     protected String JavaDoc expectedType = null;
34     protected Map JavaDoc fields = new HashMap JavaDoc();
35     protected Entry entry = null;
36
37     public JavaSpaceTemplateFilter()
38     {
39         super();
40     }
41
42     public JavaSpaceTemplateFilter(String JavaDoc expectedType)
43     {
44         setExpectedType(expectedType);
45     }
46
47     /**
48      * Check a given message against this filter.
49      *
50      * @param message a non null message to filter.
51      * @return <code>true</code> if the message matches the filter
52      */

53     public boolean accept(UMOMessage message)
54     {
55         // This filter is Used to configure a template on a space endpoint
56
return true;
57     }
58
59     public String JavaDoc getExpectedType()
60     {
61         return expectedType;
62     }
63
64     public void setExpectedType(String JavaDoc expectedType)
65     {
66         if (NULL_VALUE.equalsIgnoreCase(expectedType) || StringUtils.isEmpty(expectedType))
67         {
68             expectedType = null;
69         }
70         else
71         {
72             this.expectedType = expectedType;
73         }
74     }
75
76     public Map JavaDoc getFields()
77     {
78         return fields;
79     }
80
81     public void setFields(Map JavaDoc fields)
82     {
83         for (Iterator JavaDoc iterator = fields.entrySet().iterator(); iterator.hasNext();)
84         {
85             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
86             setProperty(entry.getKey(), entry.getValue());
87         }
88     }
89
90     public Entry getEntry()
91         throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc,
92         InstantiationException JavaDoc, ClassNotFoundException JavaDoc
93     {
94
95         if (entry == null)
96         {
97             if (expectedType == null)
98             {
99                 return null; // Match all template
100
}
101             Object JavaDoc entryType = ClassUtils.instanciateClass(expectedType, ClassUtils.NO_ARGS);
102             if (!(entryType instanceof Entry))
103             {
104                 entry = new JiniMessage(null, entryType);
105                 if (fields.size() > 0)
106                 {
107                     // Populate any properties on the message
108
BeanUtils.populate(entry, fields);
109                     // populate properties on the payload
110
BeanUtils.populate(((JiniMessage)entry).getPayload(), fields);
111                 }
112             }
113             else
114             {
115                 entry = (Entry)entryType;
116                 if (fields.size() > 0)
117                 {
118                     BeanUtils.populate(entry, fields);
119                 }
120             }
121
122         }
123         return entry;
124     }
125
126     public void setEntry(Entry entry)
127     {
128         this.entry = entry;
129     }
130
131     // Useful when configuring the filter programmatically
132
public Object JavaDoc getProperty(Object JavaDoc key)
133     {
134         return fields.get(key);
135     }
136
137     // Useful when configuring the filter programmatically
138
public void setProperty(Object JavaDoc key, Object JavaDoc value)
139     {
140         if (NULL_VALUE.equalsIgnoreCase(value.toString()))
141         {
142             fields.put(key, null);
143         }
144         else
145         {
146             fields.put(key, value);
147         }
148     }
149
150     public String JavaDoc toString()
151     {
152         return "Filter{" + "expectedType=" + expectedType + ", fields=" + fields + "}";
153     }
154 }
155
Popular Tags