1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.webdav.java;
14
15 import org.abstracthorizon.danube.beanconsole.BeanHelper;
16 import org.abstracthorizon.danube.http.util.IOUtils;
17
18 import java.beans.BeanInfo;
19 import java.beans.IntrospectionException;
20 import java.beans.Introspector;
21 import java.beans.PropertyDescriptor;
22 import java.beans.PropertyEditor;
23 import java.beans.PropertyEditorManager;
24 import java.io.ByteArrayOutputStream;
25 import java.io.OutputStream;
26 import java.lang.reflect.Method;
27
28
29
30
31
32
33
34
35
36 public class Property extends StringDelegate {
37
38
39 protected String propertyName;
40
41
42
43
44
45 public Property(String path) {
46 super(path);
47 propertyName = IOUtils.lastPathComponent(path);
48 int i = propertyName.lastIndexOf('.');
49 if (i >= 0) {
50 propertyName = propertyName.substring(0, i);
51 }
52 }
53
54
55
56
57
58
59 protected String getValueAsString(JavaWebDAVResourceAdapter adapter) {
60 Object object = adapter.findObjectImpl(objectPath);
61 if (object != null) {
62 return BeanHelper.getPropertyValueAsString(object, propertyName);
63 } else {
64 return null;
65 }
66 }
67
68
69
70
71
72
73 protected boolean setValueAsString(JavaWebDAVResourceAdapter adapter, String value) {
74 Object object = adapter.findObjectImpl(objectPath);
75 if (object != null) {
76 boolean hasError = false;
77 StringBuilder errorString = null;
78 try {
79 BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
80
81 PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
82 for (PropertyDescriptor prop : props) {
83 if (prop.getName().equals(propertyName)) {
84 Method writeMethod = prop.getWriteMethod();
85 if (writeMethod != null) {
86 PropertyEditor writePropertyEditor = PropertyEditorManager.findEditor(writeMethod.getParameterTypes()[0]);
87 if (writePropertyEditor != null) {
88 String oldValue = BeanHelper.toString(prop);
89 if (oldValue != value) {
90 try {
91 writePropertyEditor.setAsText(value);
92 writeMethod.invoke(object, new Object[]{writePropertyEditor.getValue()});
93 return true;
94 } catch (Exception e) {
95 if (!hasError) {
96 hasError = true;
97 errorString = new StringBuilder();
98 } else {
99 errorString.append("\n");
100 }
101 errorString.append(BeanHelper.stackTrace(e));
102 }
103 } else {
104
105 }
106 }
107 }
108 }
109 }
110 } catch (IntrospectionException e) {
111 if (errorString == null) {
112 errorString = new StringBuilder();
113 }
114 if (!hasError) {
115 hasError = true;
116 errorString = new StringBuilder();
117 } else {
118 errorString.append("\n");
119 }
120 errorString.append(BeanHelper.stackTrace(e));
121 }
122
123 } else {
124
125 }
126 return false;
127 }
128
129
130
131
132
133
134 public OutputStream getOutputStream(final JavaWebDAVResourceAdapter adapter) {
135 ByteArrayOutputStream outputStream = new ByteArrayOutputStream() {
136
137 public void close() {
138 String value = new String(toByteArray());
139 setValueAsString(adapter, value);
140 }
141
142 };
143 return outputStream;
144 }
145 }