View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * This class defines property value as a WebDAV's resource (file).
30   * Reading the file will return object's property content as a string,
31   * while writing to the file will change (if possible) property's value
32   * from the string (using property editors defined)
33   *
34   * @author Daniel Sendula
35   */
36  public class Property extends StringDelegate {
37  
38      /** Cached property name */
39      protected String propertyName;
40  
41      /**
42       * Constructor
43       * @param path path to the property
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       * Reads property as a string value
56       * @param adapter
57       * @return property value as a string
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       * This method sets property's value from a given string
70       * @param adapter adapter
71       * @param value value as a string
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                                      //logger.debug("Skipped storing new value for " + param + " since they are same");
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 //            return null;
125         }
126         return false;
127     }
128 
129     /**
130      * Returns an output stream to write value to.
131      * @param adapter adapter
132      * @return output stream
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 }