All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beangle.inject.spring.config.ResourcesEditor Maven / Gradle / Ivy

The newest version!
/*
 * Beangle, Agile Development Scaffold and Toolkits.
 *
 * Copyright © 2005, The Beangle Software.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package org.beangle.inject.spring.config;

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.beangle.commons.collection.CollectUtils;
import org.beangle.commons.inject.Resources;
import org.beangle.commons.lang.Strings;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class ResourcesEditor extends PropertyEditorSupport {

  private final ResourceLoader resourceLoader;

  private final PathMatchingResourcePatternResolver resourcePatternResolver;

  public ResourcesEditor() {
    super();
    resourceLoader = new DefaultResourceLoader();
    resourcePatternResolver = new PathMatchingResourcePatternResolver();
  }

  private URL getResource(String location) {
    if (Strings.isBlank(location)) return null;
    Resource res = resourceLoader.getResource(location);
    try {
      return res.exists() ? res.getURL() : null;
    } catch (IOException e) {
    }
    return null;
  }

  private List getResources(String locationPattern) {
    List locals = CollectUtils.newArrayList();
    try {
      Resource[] reses = resourcePatternResolver.getResources(locationPattern);
      for (Resource res : reses)
        if (res.exists()) locals.add(res.getURL());
    } catch (IOException e) {
    }
    return locals;
  }

  public void setAsText(String text) {
    if (Strings.isNotBlank((String) text)) {
      Resources resources = new Resources();
      String[] paths = text.split(";");
      if (paths.length > 0) resources.setGlobal(getResource(paths[0]));
      if (paths.length > 1) resources.setLocals(getResources(paths[1]));
      if (paths.length > 2) resources.setUser(getResource(paths[2]));
      setValue(resources);
    } else {
      setValue(null);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy