001//
002// Name
003//      $RCSfile: Notifier.java,v $
004//
005// COPYRIGHT
006//      Copyright 2009-2015 Cloud Software Group, Inc. ALL RIGHTS RESERVED.
007//      Cloud Software Group, Inc. Confidential Information
008//
009// History
010//      $Revision: 1.1.2.20 $ $Date: 2015/01/20 00:32:10 $
011//      $Source: /opt/cvs-dtm/switchcore/ktvm/component/com/kabira/platform/component/Attic/Notifier.java,v $
012//
013package com.kabira.platform.component;
014
015import com.kabira.platform.classloader.ClassLoader;
016import com.kabira.platform.property.Status;
017import java.io.File;
018import java.net.URL;
019
020/**
021 * Component state change notifier class. <p> A Notifier class may choose to
022 * override any of the methods. The default implementation does nothing. <p>
023 * Notifier methods are called from within a transaction.
024 */
025public class Notifier
026{
027    static final String className = "com.kabira.platform.component.Notifier";
028
029    private final com.kabira.platform.classloader.ClassLoader m_classLoader;
030    
031    private static final char WindowsSeparator = '\\';
032    private static final char UnixSeparator = '/';
033    private static final String TargetNameProperty =
034            "com.kabira.platform.component.target.name";
035    private static final String TargetParametersProperty =
036            "com.kabira.platform.component.target.parameters";
037    private static final String TargetParametersPropertySeparator = ",";
038
039    /**
040     * Prevent direct instantiation.
041     */
042    protected Notifier()
043    {
044        m_classLoader = (ClassLoader) getClass().getClassLoader();
045
046        // we are not reading the TargetName and TargetNameParmeters
047        // properties here, to allow for some testing within a single
048        // JVM run in componenttest
049    }
050
051    /**
052     * Called before any configuration files for the component currently being
053     * activated have been loaded and activated.
054     *
055     * @throws ComponentException Used to communicate a fatal initialize error.
056     */
057    protected void preConfigurationInitialize() throws ComponentException
058    {
059    }
060
061    /**
062     * Called after all configuration files for the component currently being
063     * activated have been loaded and activated.
064     *
065     * @throws ComponentException Used to communicate a fatal initialization
066     * error.
067     */
068    protected void postConfigurationInitialize() throws ComponentException
069    {
070    }
071
072    /**
073     * Called during component deactivation before any of the configuration
074     * files from this component have been deactivated and unloaded.
075     */
076    protected void preConfigurationTerminate()
077    {
078    }
079
080    /**
081     * Called during component deactivation after all of the configuration files
082     * from this component have been deactivated and unloaded.
083     */
084    protected void postConfigurationTerminate()
085    {
086    }
087
088    /**
089     * Returns the name of the target provided to the deploy tool when the
090     * current JVM was deployed.
091     *
092     * @return target name.
093     */
094    protected final String getTargetName()
095    {
096        return getFileName(getFullTargetName());
097    }
098
099    /**
100     * Returns a handle to a local copy of the target archive or file
101         * specified to the
102     * deploy tool when the current JVM was deployed.
103     *
104     * Returns null if the current target is not an archive or file.
105     *
106     * @return target archive or file
107     */
108    protected final File getTargetArchive()
109    {
110        String targetName = getTargetName();
111        
112        // if detached, it may have been copied to the JVM's classpath
113        // directory, or if not there, then it has to be in the deploy
114        // directories.
115
116        URL url = m_classLoader.getResource(getFileName(targetName));
117
118        if (url == null)
119        {
120            return ComponentUtils.getFileFromDeployDirectories(
121                getFileName(targetName));
122        }
123
124        return new File(url.getFile());
125    }
126
127    /**
128     * Returns a handle to a local copy of the requested file.
129     * 
130     * This method will be removed from the public interface in a 
131     * future release.  There is no replacement.
132     *
133     * @param resourceName Name of the file to find. Path information
134     *                     is ignored.
135     *
136     * @return The copy of the file. null is returned if the file cannot be
137     * found.
138     * 
139     * @throws ComponentException - no longer used.
140     */
141    @Deprecated
142    protected final File getResource(final String resourceName)
143            throws ComponentException
144    {
145        Log.debug("getResource(" + resourceName + ")");
146
147        String fileName = getFileName(resourceName);
148            
149        // The client either already copied the file to the application
150        // classpath folder.
151
152        URL url = m_classLoader.getResource(fileName);
153
154        // Or the user put it in a deploy directory.
155        if (url == null)
156        {
157            return ComponentUtils.getFileFromDeployDirectories(fileName);
158        }
159
160        return new File(url.getFile());
161    }
162
163    /**
164     * Returns the application parameters provided to the deploy tool when the
165     * current JVM was deployed.
166     *
167     * @return deployment tool application parameters
168     */
169     protected final String[] getApplicationParameters()
170    {
171        String parameters = getTargetParametersPropertyValue();
172        
173        if (parameters == null)
174        {
175            return null;
176        }
177        
178        return parameters.split(TargetParametersPropertySeparator);
179    }
180
181    /**
182     * Returns a server-side directory which can be used for creating JVM
183     * specific artifacts.
184         * <p>
185         * Null is returned if the output directory found or created.
186     *
187     * @return The output directory for the calling JVM.
188     */
189    protected final File getOutputDirectory()
190    {
191        final String errorPrefix = "getOutputDirectory failure: ";
192        final String outputDirName = System.getProperty(Status.CLASS_PATH);
193
194        if (outputDirName == null)
195        {
196            Log.warning(errorPrefix + "cannot find directory");
197            return null;
198        }
199
200        File outputDir = new File(outputDirName);
201
202        if (!(outputDir.isDirectory()
203                && outputDir.canExecute()
204                && outputDir.canRead()
205                && outputDir.canWrite()))
206        {
207            Log.warning(errorPrefix
208                    + "directory permission problems on " + outputDirName);
209            return null;
210        }
211
212        return outputDir;
213    }
214
215    /**
216     * Adds to the class path of the current JVM.
217     *
218     * @param file The jar or directory to add to the class path.
219     */
220    protected final void addToClassPath(final File file)
221    {
222        m_classLoader.addToClasspath(file.getPath());
223    }
224
225    private String getFileName(final String fileAndPath)
226    {
227        // fileAndPath may contain client side path information, from
228        // either Windows or a Unix client.   Break it into path and file
229        // name.
230
231        int lastUnixPos = fileAndPath.lastIndexOf(UnixSeparator);
232        int lastWindowsPos = fileAndPath.lastIndexOf(WindowsSeparator);
233        int lastPos = Math.max(lastUnixPos, lastWindowsPos);
234
235        String fileName = fileAndPath;
236
237        if (lastPos >= 0)
238        {
239            assert (fileAndPath.length() > lastPos);
240            fileName = fileAndPath.substring(lastPos + 1);
241        }
242
243        return fileName;
244    }
245    
246    private String getFullTargetName()
247    {
248        String targetName = getTargetNamePropertyValue();
249  
250        assert targetName != null;
251        
252        return getFileName(targetName);
253    }
254    
255    private String getTargetNamePropertyValue()
256    {
257        return System.getProperty(TargetNameProperty);
258    }
259    
260    private String getTargetParametersPropertyValue()
261    {
262        return System.getProperty(TargetParametersProperty);
263    }
264}