001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt.lib;
009    
010    import java.util.List;
011    
012    import org.slf4j.Logger;
013    import org.slf4j.LoggerFactory;
014    
015    /**
016     * default library loader implementation;
017     * <p>
018     * tries to load native libraries by extracting them from from 3 possible class
019     * path locations, in the following order:
020     * <p>
021     * 1) release : JAR packaged library
022     * <p>
023     * 2) staging : NAR exploded class path library
024     * <p>
025     * 3) testing : CDT exploded class path library
026     */
027    public class LibraryLoaderUDT implements LibraryLoader {
028    
029            private final static Logger log = LoggerFactory
030                            .getLogger(LibraryLoaderUDT.class);
031    
032            /**
033             * load using provided extract location
034             */
035            @Override
036            public void load(final String targetFolder) throws Exception {
037    
038                    if (PluginPropsUDT.isSupportedPlatform()) {
039                            log.info("Platform supported.");
040                    } else {
041                            throw new IllegalStateException("Unsupported platform.");
042                    }
043    
044                    if (targetFolder == null || targetFolder.length() == 0) {
045                            throw new IllegalStateException("Invalid extract location.");
046                    }
047    
048                    try {
049                            log.info("Loading release libraries.");
050                            loadRelease(targetFolder);
051                            log.info("Release libraries loaded.");
052                            return;
053                    } catch (final Throwable e) {
054                            log.warn("Release libraries missing: {}", e.getMessage());
055                    }
056    
057                    try {
058                            log.info("Loading staging libraries.");
059                            loadStaging(targetFolder);
060                            log.info("Staging libraries loaded.");
061                            return;
062                    } catch (final Throwable e) {
063                            log.warn("Staging libraries missing: {}", e.getMessage());
064                    }
065    
066                    try {
067                            log.info("Loading testing libraries.");
068                            loadTesting(targetFolder);
069                            log.info("Testing libraries loaded.");
070                            return;
071                    } catch (final Throwable e) {
072                            log.warn("Testing libraries missing: {}", e.getMessage());
073                    }
074    
075                    throw new IllegalStateException("Fatal: library load failed.");
076    
077            }
078    
079            protected void loadAll(final List<String> sourceList,
080                            final String targetFolder) throws Exception {
081    
082                    /** extract all libraries or fail */
083                    for (final String sourcePath : sourceList) {
084                            final String targetPath = targetFolder + sourcePath;
085                            ResourceManagerUDT.extractResource(sourcePath, targetPath);
086                    }
087    
088                    /** try to load only if all are extracted */
089                    for (final String sourcePath : sourceList) {
090                            final String targetPath = targetFolder + sourcePath;
091                            ResourceManagerUDT.systemLoad(targetPath);
092                    }
093    
094            }
095    
096            /** try to load from JAR class path library */
097            protected void loadRelease(final String targetFolder) throws Exception {
098    
099                    final String coreName = VersionUDT.BARCHART_NAME;
100    
101                    final List<String> sourceList = PluginPropsUDT
102                                    .currentReleaseLibraries(coreName);
103    
104                    loadAll(sourceList, targetFolder);
105    
106            }
107    
108            /** try to load from NAR exploded class path library */
109            protected void loadStaging(final String targetFolder) throws Exception {
110    
111                    final String coreName = VersionUDT.BARCHART_NAME;
112    
113                    final List<String> sourceList = PluginPropsUDT
114                                    .currentStagingLibraries(coreName);
115    
116                    loadAll(sourceList, targetFolder);
117    
118            }
119    
120            /** try to load from CDT exploded class path library */
121            protected void loadTesting(final String targetFolder) throws Exception {
122    
123                    final String coreName = VersionUDT.BARCHART_ARTIFACT + "-"
124                                    + PluginPropsUDT.currentNarPath();
125    
126                    final List<String> sourceList = PluginPropsUDT
127                                    .currentTestingLibraries(coreName);
128    
129                    loadAll(sourceList, targetFolder);
130    
131            }
132    
133    }