AdminResource.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * NorthernWind - lightweight CMS
  6.  * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
  7.  * %%
  8.  * Copyright (C) 2013 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  * *********************************************************************************************************************
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  13.  * the License. You may obtain a copy of the License at
  14.  *
  15.  *     http://www.apache.org/licenses/LICENSE-2.0
  16.  *
  17.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  18.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations under the License.
  20.  *
  21.  * *********************************************************************************************************************
  22.  *
  23.  *
  24.  * *********************************************************************************************************************
  25.  * #L%
  26.  */
  27. package it.tidalwave.northernwind.model.impl.admin;

  28. import javax.annotation.Nonnull;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import it.tidalwave.util.NotFoundException;
  32. import it.tidalwave.northernwind.core.model.Resource;
  33. import it.tidalwave.northernwind.core.model.ResourceFile;
  34. import it.tidalwave.northernwind.core.model.ResourceProperties;
  35. import it.tidalwave.northernwind.core.model.spi.ResourceSupport;
  36. import lombok.Cleanup;
  37. import lombok.extern.slf4j.Slf4j;
  38. import static it.tidalwave.role.io.Unmarshallable.*;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. @Slf4j
  45. public class AdminResource extends ResourceSupport
  46.   {
  47.     @Nonnull
  48.     private final PatchedTextResourcePropertyResolver propertyResolver;

  49.     public AdminResource (final @Nonnull Resource.Builder builder)
  50.       {
  51.         super(builder);
  52.         propertyResolver = new PatchedTextResourcePropertyResolver(getFile());
  53.       }

  54.     /*******************************************************************************************************************
  55.      *
  56.      * {@inheritDoc}
  57.      *
  58.      ******************************************************************************************************************/
  59.     @Override @Nonnull
  60.     public ResourceProperties getProperties()
  61.       {
  62.         try
  63.           {
  64.             return loadProperties();
  65.           }
  66.         catch (IOException e)
  67.           {
  68.             throw new RuntimeException(e);
  69.           }
  70.       }

  71.     @Override
  72.     public boolean isPlaceHolder()
  73.       {
  74.         throw new UnsupportedOperationException("Not supported yet.");
  75.       }

  76.     /*******************************************************************************************************************
  77.      *
  78.      *
  79.      ******************************************************************************************************************/
  80.     @Nonnull
  81.     private ResourceProperties loadProperties()
  82.       throws IOException
  83.       {
  84.         final ResourceFile file = getFile();
  85.         log.debug("loadProperties() for {}", file.getPath().asString());

  86.         ResourceProperties properties = modelFactory.createProperties().withPropertyResolver(propertyResolver).build();

  87.         try
  88.           {
  89.             final ResourceFile propertyFile = file.findChildren().withName("Properties.xml").result(); // FIXME reuse the inheritance helper
  90.     //        log.trace(">>>> reading properties from {} ({})...", propertyFile.getPath().asString(), locale);
  91.             @Cleanup final InputStream is = propertyFile.getInputStream();
  92.             final ResourceProperties tempProperties =
  93.     //            modelFactory.createProperties().build().as(Unmarshallable).unmarshal(is);
  94.                     modelFactory.createProperties().withPropertyResolver(propertyResolver).build().as(Unmarshallable).unmarshal(is);
  95.     //        log.trace(">>>>>>>> read properties: {} ({})", tempProperties, locale);
  96.             properties = properties.merged(tempProperties);
  97.           }
  98.         catch (NotFoundException e)
  99.           {
  100.             // ok, no properties
  101.           }

  102.         return properties;
  103.       }
  104.   }