Example usage for org.springframework.context ApplicationContext getResource

List of usage examples for org.springframework.context ApplicationContext getResource

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getResource.

Prototype

Resource getResource(String location);

Source Link

Document

Return a Resource handle for the specified resource location.

Usage

From source file:com.home.ln_spring.ch5.event.ResourceDemo.java

public static void main(String[] args) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:events/events.xml");

    Resource res1 = ac.getResource("file:/home/vitaliy/NetBeansProjects/Ln_Spring/src/main/resources/test.txt");
    displayInfo(res1);//from  www.  ja  v  a 2s  .co m

    Resource res2 = ac.getResource("classpath:test.txt");
    displayInfo(res2);

    Resource res3 = ac.getResource("http://www.google.com");
    displayInfo(res3);

}

From source file:de.alpharogroup.duplicate.files.panels.progressbar.ImagePanelTest.java

/**
 * The main method.//from   w  w w.  j  a v a2 s.c om
 *
 * @param args the arguments
 */
public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.addWindowListener(new CloseWindow());
    frame.setTitle("ImagePanelTest");

    ApplicationContext ctx = SpringApplicationContext.getInstance().getApplicationContext();
    Resource resource = ctx.getResource("classpath:images/EngineHierarchy.PNG");

    Resource log4jconfig = ctx.getResource("classpath:conf/log4j/log4jconfig.xml");

    try {
        DOMConfigurator.configure(log4jconfig.getURL());
    } catch (FactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    File imageFile = null;
    try {
        imageFile = resource.getFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    BufferedImage image = null;
    try {
        image = javax.imageio.ImageIO.read(imageFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ImagePanel pnlIconPanel = new ImagePanel(image);
    frame.add(pnlIconPanel);
    frame.setBounds(0, 0, 534, 336);
    frame.setVisible(true);
}

From source file:de.alpharogroup.duplicate.files.desktoppane.MainApplication.java

/**
 * The main method./* ww w.j  a  v  a  2 s. c om*/
 *
 * @param args
 *            the arguments
 */
public static void main(final String[] args) {

    final ApplicationContext ctx = SpringApplicationContext.getInstance().getApplicationContext();
    final Resource resource = ctx.getResource("classpath:conf/log4j/log4jconfig.xml");

    try {
        DOMConfigurator.configure(resource.getURL());
    } catch (final FactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final MainFrame mainFrame = MainFrame.getInstance();
    final DesktopMenu menu = DesktopMenu.getInstance();
    mainFrame.setJMenuBar(menu.getMenubar());

    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setSize(ScreenSizeExtensions.getScreenWidth(), ScreenSizeExtensions.getScreenHeight());
    mainFrame.setVisible(true);
    mainFrame.getDesktopPane().getDesktopManager().activateFrame(mainFrame.getInternalFrame());
    mainFrame.getDesktopPane().getDesktopManager().maximizeFrame(mainFrame.getInternalFrame());
    mainFrame.getInternalFrame().toFront();

    // Set default look and feel...
    try {
        UIManager.setLookAndFeel(LookAndFeels.SYSTEM.getLookAndFeelName());
        SwingUtilities.updateComponentTreeUI(mainFrame);
        mainFrame.setCurrentLookAndFeels(LookAndFeels.SYSTEM);
    } catch (final ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (final InstantiationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (final IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (final UnsupportedLookAndFeelException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

From source file:com.otz.transport.common.config.ContentUtil.java

public static String getStringContent(ApplicationContext context, String fileName) throws IOException {

    Resource resource = context.getResource("classpath:/" + fileName);
    int availableByte = resource.getInputStream().available();
    byte[] contentBytes = new byte[availableByte];
    resource.getInputStream().read(contentBytes);
    return new String(contentBytes);
}

From source file:org.reunionemu.jreunion.server.Reference.java

public static InputStream getDataResource(String filename) {
    String dataPath = getInstance().getServerReference().getItem("Server").getMemberValue("DataPath");
    ApplicationContext context = getInstance().getContext();
    String path = new File(dataPath, filename).getPath();
    try {/*from  w  w w  .j a v a2  s  . c o m*/
        if (context != null) {
            return context.getResource(path).getInputStream();
        } else {
            return new FileInputStream(path);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.betfair.tornjak.monitor.overlay.AuthUtils.java

private static synchronized RolePerms getOrCreateRolePerms(ServletContext servletContext) throws IOException {
    RolePerms rolePerms;/*ww  w . jav  a  2s.co m*/

    rolePerms = (RolePerms) servletContext.getAttribute(CONTEXT_ATTR_NAME);

    if (rolePerms != null) {
        return rolePerms;
    }

    ApplicationContext context = (ApplicationContext) servletContext
            .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    if (context == null) {
        throw new ApplicationContextException("Unable to find application context");
    }
    String authFileLocation = servletContext.getInitParameter(CONTEXT_INIT_PARAM_NAME);
    if (StringUtils.isBlank(authFileLocation)) {
        throw new ApplicationContextException(String
                .format("Parameter '%s' not defined, unable to load jmx auth file", CONTEXT_INIT_PARAM_NAME));
    }

    rolePerms = AuthFileReader.load(context.getResource(authFileLocation));
    servletContext.setAttribute(CONTEXT_ATTR_NAME, rolePerms);

    return rolePerms;

}

From source file:org.xmlactions.common.io.ResourceUtils.java

/**
 * Get a URL for a given resource. The resource must exist or an exception
 * is caused.//from w  w  w  .  j a  va 2  s .c  o m
 * 
 * @param resourceName
 * @return the URL of the resource.
 */
public static URL getResourceURL(ApplicationContext appContext, String resourceName, Class<?> clas) {
    URL resource = null;
    try {
        if (appContext != null) {
            resource = appContext.getResource(resourceName).getURL();
        } else {
            ClassPathResource cpr = new ClassPathResource(resourceName);
            resource = cpr.getURL();
        }

        if (resource == null) {
            resource = ResourceUtils.class.getResource(resourceName);
            if (resource == null) {
                resource = clas.getResource(resourceName);
            }
        }
    } catch (IOException ex) {
        // TODO Auto-generated catch block
        throw new IllegalArgumentException("Unable to load resource [" + resourceName + "]", ex);
    }
    Validate.notNull(resource, "Unable to load resource [" + resourceName + "]");

    return resource;
}

From source file:com.toyota.carservice.SplashScreen.java

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/com/toyota/carservice/view/formSplash.fxml"));
    Scene scene = new Scene(root);
    stage.setTitle("Connection to database...");
    ApplicationContext appContex = config.getInstance().getApplicationContext();
    Resource resource = appContex.getResource("classpath:com/toyota/carservice/img/kallatoyota.png");
    stage.getIcons().add(new Image(resource.getURI().toString()));
    stage.setScene(scene);//from   w w w  .  j a v a2  s  .c o  m
    stage.initStyle(StageStyle.UNDECORATED);
    stage.show();
}

From source file:org.openmrs.cwf.api.util.OpenMRSInit.java

@Override
public void setApplicationContext(ApplicationContext appContext) throws BeansException {
    Resource resource = appContext.getResource(propertiesFile);

    InputStream is = null;//from  w  ww .  ja  va2 s  . c  o m

    try {
        is = resource.getInputStream();
        properties.load(is);
    } catch (Exception e) {
        log.error("Error loading startup properties file: " + propertiesFile, e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.powertac.genco.GencoConfigTest.java

/**
 *
 *//*ww  w . ja v  a  2 s  . c  o m*/
@Before
public void setUp() throws Exception {
    ApplicationContext context = SpringApplicationContext.getContext();
    Resource props = context.getResource("genco-test.properties");
    // this probably won't work if tests are packaged in a jarfile
    config = new PropertiesConfiguration(props.getFile());
}