File Source: WindowsMediaSourcePlugin.java
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.mediasource_windows;
24
25 import com.dmdirc.Main;
26 import com.dmdirc.addons.nowplaying.MediaSource;
27 import com.dmdirc.addons.nowplaying.MediaSourceManager;
28 import com.dmdirc.logger.ErrorLevel;
29 import com.dmdirc.logger.Logger;
30 import com.dmdirc.plugins.Plugin;
31 import com.dmdirc.plugins.PluginInfo;
32 import com.dmdirc.plugins.PluginManager;
33 import com.dmdirc.util.resourcemanager.ResourceManager;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import java.io.File;
40 import java.io.IOException;
41
42 import com.dmdirc.installer.StreamReader;
43 /**
44 * Manages all Windows based media sources.
45 */
46 public class WindowsMediaSourcePlugin extends Plugin implements MediaSourceManager {
47 /** Media sources. */
48 private final List<MediaSource> sources;
49 /** Files dir */
/*
P/P * Method: com.dmdirc.addons.mediasource_windows.WindowsMediaSourcePlugin__static_init
*
* Postconditions:
* "plugins.windowsmediasource_files."._tainted == 0
* java.lang.StringBuilder:toString(...)._tainted == 0
* filesDir == &java.lang.StringBuilder:toString(...)
*/
50 private static final String filesDir = Main.getConfigDir() + "plugins/windowsmediasource_files/";
51
52 /**
53 * Creates a new instance of DcopMediaSourcePlugin.
54 */
55 public WindowsMediaSourcePlugin() {
/*
P/P * Method: void com.dmdirc.addons.mediasource_windows.WindowsMediaSourcePlugin()
*
* Postconditions:
* this.sources == &new ArrayList(WindowsMediaSourcePlugin#1)
* new ArrayList(WindowsMediaSourcePlugin#1) num objects == 1
*/
56 super();
57 sources = new ArrayList<MediaSource>();
58 sources.add(new DllSource("Winamp", true));
59 sources.add(new DllSource("iTunes", false));
60 }
61
62 /** {@inheritDoc} */
63 @Override
64 public List<MediaSource> getSources() {
/*
P/P * Method: List getSources()
*
* Postconditions:
* return_value == this.sources
* init'ed(return_value)
*/
65 return sources;
66 }
67
68 /**
69 * Get the output from GetMediaInfo.exe for the given player and method
70 *
71 * @param player Player to ask about
72 * @param method Method to call
73 * @return a MediaInfoOutput with the results
74 */
75 protected static MediaInfoOutput getOutput(final String player, final String method) {
76 try {
/*
P/P * Method: MediaInfoOutput getOutput(String, String)
*
* Presumptions:
* java.lang.Runtime:exec(...)@77 != null
* java.lang.Runtime:getRuntime(...)@77 != null
*
* Postconditions:
* java.lang.StringBuffer:toString(...)._tainted == 0
* return_value in Addr_Set{&new MediaInfoOutput(getOutput#6),&new MediaInfoOutput(getOutput#7)}
* new MediaInfoOutput(getOutput#6) num objects <= 1
* init'ed(new MediaInfoOutput(getOutput#6).exitCode)
* new MediaInfoOutput(getOutput#6).output == &java.lang.StringBuffer:toString(...)
* new MediaInfoOutput(getOutput#7) num objects <= 1
* new MediaInfoOutput(getOutput#7).exitCode == -1
* new MediaInfoOutput(getOutput#7).output == &"Error executing GetMediaInfo.exe"
*/
77 final Process myProcess = Runtime.getRuntime().exec(new String[]{filesDir+"GetMediaInfo.exe", player, method});
78 final StringBuffer data = new StringBuffer();
79 new StreamReader(myProcess.getErrorStream()).start();
80 new StreamReader(myProcess.getInputStream(), data).start();
81 try {
82 myProcess.waitFor();
83 } catch (InterruptedException e) { }
84
85 return new MediaInfoOutput(myProcess.exitValue(),data.toString());
86 } catch (SecurityException e) {
87 } catch (IOException e) {
88 }
89
90 return new MediaInfoOutput(-1, "Error executing GetMediaInfo.exe");
91 }
92
93 /**
94 * Use the given resource manager to extract files ending with the given suffix
95 *
96 * @param res ResourceManager
97 * @param newDir Directory to extract to.
98 * @param suffix Suffix to extract
99 */
100 private void extractFiles(final ResourceManager res, final File newDir, final String suffix) {
/*
P/P * Method: void extractFiles(ResourceManager, File, String)
*
* Preconditions:
* res != null
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.LOW)
* com.dmdirc.util.resourcemanager.ResourceManager:getResourceManager(...)@111 != null
* com.dmdirc.util.resourcemanager.ResourceManager:getResourcesEndingWithAsBytes(...)@101 != null
* java.util.Iterator:next(...)@102 != null
* java.util.Map:entrySet(...)@102 != null
* ...
*
* Test Vectors:
* java.io.File:exists(...)@110: {0}, {1}
* java.io.File:isDirectory(...)@109: {1}, {0}
* java.util.Iterator:hasNext(...)@102: {0}, {1}
*/
101 final Map<String, byte[]> resources = res.getResourcesEndingWithAsBytes(suffix);
102 for (Entry<String, byte[]> resource : resources.entrySet()) {
103 try {
104 final String key = resource.getKey();
105 final String resourceName = key.substring(key.lastIndexOf('/'), key.length());
106
107 final File newFile = new File(newDir, resourceName);
108
109 if (!newFile.isDirectory()) {
110 if (newFile.exists()) { newFile.delete(); }
111 ResourceManager.getResourceManager().resourceToFile(resource.getValue(), newFile);
112 }
113 } catch (IOException ex) {
114 Logger.userError(ErrorLevel.LOW, "Failed to extract "+suffix+"s for windowsmediasource: "+ex.getMessage(), ex);
115 }
116 }
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public void onLoad() {
122 // Extract GetMediaInfo.exe and required DLLs
/*
P/P * Method: void onLoad()
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.LOW)
* com.dmdirc.plugins.PluginInfo:getResourceManager(...)@130 != null
* com.dmdirc.plugins.PluginManager:getPluginManager(...)@123 != null
*
* Test Vectors:
* com.dmdirc.plugins.PluginManager:getPluginInfoByName(...)@123: Inverse{null}, Addr_Set{null}
* java.io.File:exists(...)@134: {1}, {0}
*/
123 final PluginInfo pi = PluginManager.getPluginManager().getPluginInfoByName("windowsmediasource");
124
125 // This shouldn't actually happen, but check to make sure.
126 if (pi == null) { return; }
127
128 // Now get the RM
129 try {
130 final ResourceManager res = pi.getResourceManager();
131
132 // Make sure our files dir exists
133 final File newDir = new File(filesDir);
134 if (!newDir.exists()) {
135 newDir.mkdirs();
136 }
137
138 // Now extract the .dlls and .exe
139 extractFiles(res, newDir, ".dll");
140 extractFiles(res, newDir, ".exe");
141 } catch (IOException ioe) {
142 Logger.userError(ErrorLevel.LOW, "Unable to open ResourceManager for windowsmediasource: "+ioe.getMessage(), ioe);
143 }
144 }
145
146 /** {@inheritDoc} */
147 @Override
/*
P/P * Method: void onUnload()
*/
148 public void onUnload() { /* Do Nothing */ }
149 }
SofCheck Inspector Build Version : 2.17854
| WindowsMediaSourcePlugin.java |
2009-Jun-25 01:54:24 |
| WindowsMediaSourcePlugin.class |
2009-Sep-02 17:04:15 |