PageRenderTime 81ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/roller-5.0.1/planet-business/src/main/java/org/apache/roller/planet/business/GuicePlanetProvider.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 98 lines | 40 code | 19 blank | 39 comment | 4 complexity | e8fb1984ecb63541b40a194d80e01bc8 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. The ASF licenses this file to You
  4. * under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License. For additional information regarding
  15. * copyright in this work, please see the NOTICE file in the top level
  16. * directory of this distribution.
  17. */
  18. package org.apache.roller.planet.business;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Injector;
  21. import com.google.inject.Module;
  22. import org.apache.roller.planet.config.PlanetConfig;
  23. /**
  24. * A Guice specific implementation of a PlanetProvider.
  25. */
  26. public class GuicePlanetProvider implements PlanetProvider {
  27. // Guice injector
  28. private final Injector injector;
  29. // maintain our own singleton instance of Planet
  30. private Planet planetInstance = null;
  31. /**
  32. * Instantiate a new GuicePlanetProvider using default guice module
  33. * configured in PlanetConfig via 'guice.backend.module' property.
  34. */
  35. public GuicePlanetProvider() {
  36. String moduleClassname = PlanetConfig.getProperty("guice.backend.module");
  37. if(moduleClassname == null) {
  38. throw new NullPointerException("unable to lookup default guice module via property 'guice.backend.module'");
  39. }
  40. try {
  41. Class moduleClass = Class.forName(moduleClassname);
  42. Module module = (Module)moduleClass.newInstance();
  43. injector = Guice.createInjector(module);
  44. } catch (Throwable e) {
  45. // Fatal misconfiguration, cannot recover
  46. throw new RuntimeException("Error instantiating backend module " + moduleClassname, e);
  47. }
  48. }
  49. /**
  50. * Instantiate a new GuicePlanetProvider using the given Guice module.
  51. *
  52. * @param moduleClassname The full classname of the Guice module to use.
  53. */
  54. public GuicePlanetProvider(String moduleClassname) {
  55. if(moduleClassname == null) {
  56. throw new NullPointerException("moduleClassname cannot be null");
  57. }
  58. try {
  59. Class moduleClass = Class.forName(moduleClassname);
  60. Module module = (Module)moduleClass.newInstance();
  61. injector = Guice.createInjector(module);
  62. } catch (Throwable e) {
  63. // Fatal misconfiguration, cannot recover
  64. throw new RuntimeException("Error instantiating backend module " + moduleClassname, e);
  65. }
  66. }
  67. /**
  68. * @inheritDoc
  69. */
  70. public void bootstrap() {
  71. planetInstance = injector.getInstance(Planet.class);
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. public Planet getPlanet() {
  77. return planetInstance;
  78. }
  79. }