PageRenderTime 4545ms CodeModel.GetById 19ms RepoModel.GetById 3ms app.codeStats 1ms

/rabbit.data.handler/src/rabbit/data/handler/DataHandler.java

https://code.google.com/
Java | 116 lines | 50 code | 10 blank | 56 comment | 0 complexity | a6b88a26b113cb21bbb7bf1452549654 MD5 | raw file
  1. /*
  2. * Copyright 2010 The Rabbit Eclipse Plug-in Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package rabbit.data.handler;
  17. import rabbit.data.access.IAccessor;
  18. import rabbit.data.access.model.ICommandData;
  19. import rabbit.data.access.model.IFileData;
  20. import rabbit.data.access.model.IJavaData;
  21. import rabbit.data.access.model.ILaunchData;
  22. import rabbit.data.access.model.IPartData;
  23. import rabbit.data.access.model.IPerspectiveData;
  24. import rabbit.data.access.model.ISessionData;
  25. import rabbit.data.access.model.ITaskData;
  26. import rabbit.data.store.IStorer;
  27. import rabbit.data.store.model.CommandEvent;
  28. import rabbit.data.store.model.FileEvent;
  29. import rabbit.data.store.model.JavaEvent;
  30. import rabbit.data.store.model.LaunchEvent;
  31. import rabbit.data.store.model.PartEvent;
  32. import rabbit.data.store.model.PerspectiveEvent;
  33. import rabbit.data.store.model.SessionEvent;
  34. import rabbit.data.store.model.TaskFileEvent;
  35. import rabbit.data.xml.XmlModule;
  36. import com.google.inject.ConfigurationException;
  37. import com.google.inject.Guice;
  38. import com.google.inject.Injector;
  39. import com.google.inject.Key;
  40. import com.google.inject.util.Types;
  41. /**
  42. * Handler class provider common classes to access the data.
  43. */
  44. public class DataHandler {
  45. private static final Injector injector;
  46. static {
  47. injector = Guice.createInjector(new XmlModule());
  48. }
  49. /**
  50. * Gets a storer that stores the objects of the given type.
  51. * <p>
  52. * The following object types are supported:
  53. * <ul>
  54. * <li>{@link CommandEvent}</li>
  55. * <li>{@link FileEvent}</li>
  56. * <li>{@link PartEvent}</li>
  57. * <li>{@link PerspectiveEvent}</li>
  58. * <li>{@link LaunchEvent}</li>
  59. * <li>{@link TaskFileEvent}</li>
  60. * <li>{@link SessionEvent}</li>
  61. * <li>{@link JavaEvent}</li>
  62. * </ul>
  63. * </p>
  64. *
  65. * @param clazz The class of the type.
  66. * @return A storer that stores the objects of the given type, or null.
  67. */
  68. @SuppressWarnings("unchecked")
  69. public static <T> IStorer<T> getStorer(Class<T> clazz) {
  70. try {
  71. Key<?> k = Key.get(Types.newParameterizedType(IStorer.class, clazz));
  72. return (IStorer<T>) injector.getInstance(k);
  73. } catch (ConfigurationException e) {
  74. return null;
  75. }
  76. }
  77. /**
  78. * Gets an accessor that gets the stored data.
  79. * <p>
  80. * The following object types are supported:
  81. * <ul>
  82. * <li>{@link ICommandData}</li>
  83. * <li>{@link IFileData}</li>
  84. * <li>{@link IPartData}</li>
  85. * <li>{@link IPerspectiveData}</li>
  86. * <li>{@link ILaunchData}</li>
  87. * <li>{@link ITaskData}</li>
  88. * <li>{@link ISessionData}</li>
  89. * <li>{@link IJavaData}</li>
  90. * </ul>
  91. * </p>
  92. *
  93. * @param clazz The class of the type.
  94. * @return An accessor that get the data of the given type, or null.
  95. */
  96. @SuppressWarnings("unchecked")
  97. public static <T> IAccessor<T> getAccessor(Class<T> clazz) {
  98. try {
  99. Key<?> k = Key.get(Types.newParameterizedType(IAccessor.class, clazz));
  100. return (IAccessor<T>) injector.getInstance(k);
  101. } catch (ConfigurationException e) {
  102. return null;
  103. }
  104. }
  105. private DataHandler() {}
  106. }