/plugins/ProjectViewer/tags/pv_2_1_3_3/projectviewer/persist/DeferredProperty.java

# · Java · 87 lines · 32 code · 10 blank · 45 comment · 0 complexity · b1917db42a37e76c549c4ed955be927e MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.persist;
  20. //{{{ Imports
  21. import java.io.ObjectInputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.IOException;
  26. import org.gjt.sp.util.Log;
  27. import projectviewer.PVActions;
  28. //}}}
  29. /**
  30. * Defers the loading of a property until it's requested. This avoids
  31. * ClassCastExceptions and ClassNotFoundErrors when loading PV, since
  32. * the plugins that set "object" properties might not be available.
  33. *
  34. * <p>Since in a perfect world only the plugin that set the property
  35. * will try to load it, the implementing class of the serialized object
  36. * will most probably be available when the property is requested from
  37. * the project.</p>
  38. *
  39. * <p>This requires special treatment in VPTProject.</p>
  40. *
  41. * @author Marcelo Vanzin
  42. * @version $Id: DeferredProperty.java 6386 2006-02-16 05:18:36Z vanza $
  43. * @since PV 2.1.2
  44. */
  45. public class DeferredProperty {
  46. private String data;
  47. private String name;
  48. public DeferredProperty(String data, String name) {
  49. this.data = data;
  50. this.name = name;
  51. }
  52. /**
  53. * Returns the original string with the serialized object. This will
  54. * be null if the object was instantiated.
  55. */
  56. protected String getData() {
  57. return this.data;
  58. }
  59. /**
  60. * Tries to load the object represented by the serialized data;
  61. * returns the object, or "this" is loading failed. An error message
  62. * is logged to the activity log in the latter case.
  63. */
  64. public Object getValue() {
  65. try {
  66. byte[] bytes = PVActions.decodeBase64(data);
  67. ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(bytes) );
  68. return ois.readObject();
  69. } catch (Exception e) {
  70. Log.log(Log.ERROR, this, "Error loading property of name " + name
  71. + " : " + e.getClass().getName()
  72. + " : " + e.getMessage());
  73. Log.log(Log.ERROR, this, e);
  74. }
  75. return this;
  76. }
  77. }