Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 2351181

Browse files
author
matt
committed
Fix for issue 1
1 parent f3181b4 commit 2351181

9 files changed

+81
-37
lines changed

.classpath

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<classpathentry kind="lib" path="C:/workspace/CLIMediaRenderer/httpcore-4.2.1.jar" sourcepath="C:/Users/Matt/Desktop/httpcomponents-core-4.2.1-src/httpcomponents-core-4.2.1"/>
1212
<classpathentry kind="lib" path="C:/workspace/CLIMediaRenderer/httpclient-4.2.1.jar"/>
1313
<classpathentry kind="lib" path="C:/workspace/CLIMediaRenderer/commons-logging-1.1.1.jar"/>
14-
<classpathentry kind="lib" path="C:/workspace/CLIMediaRenderer/javampd-4.1.jar"/>
14+
<classpathentry kind="lib" path="C:/workspace/CLIMediaRenderer/JMPDComm.jar"/>
1515
<classpathentry kind="output" path="bin"/>
1616
</classpath>

build.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
99
<attribute name="Rsrc-Main-Class" value="org.matt1.climediarenderer.MediaRenderer"/>
1010
<attribute name="Class-Path" value="."/>
11-
<attribute name="Rsrc-Class-Path" value="./ cling-core-2.0-20111017.171724-1.jar teleal-common-1.0.6.jar cling-support-2.0-20111017.171832-1.jar seamless-util-1.0-20111017.170503-1.jar seamless-xml-1.0-20111129.053837-2.jar seamless-http-1.0-20111017.170623-1.jar httpcore-4.2.1.jar httpclient-4.2.1.jar commons-logging-1.1.1.jar javampd-4.1.jar"/>
11+
<attribute name="Rsrc-Class-Path" value="./ cling-core-2.0-20111017.171724-1.jar teleal-common-1.0.6.jar cling-support-2.0-20111017.171832-1.jar seamless-util-1.0-20111017.170503-1.jar seamless-xml-1.0-20111129.053837-2.jar seamless-http-1.0-20111017.170623-1.jar httpcore-4.2.1.jar httpclient-4.2.1.jar commons-logging-1.1.1.jar JMPDComm.jar"/>
1212
</manifest>
1313
<zipfileset src="jar-in-jar-loader.zip"/>
1414
<fileset dir="C:/workspace/CLIMediaRenderer/bin"/>
@@ -21,7 +21,7 @@
2121
<zipfileset dir="C:\workspace\CLIMediaRenderer" includes="httpcore-4.2.1.jar"/>
2222
<zipfileset dir="C:\workspace\CLIMediaRenderer" includes="httpclient-4.2.1.jar"/>
2323
<zipfileset dir="C:\workspace\CLIMediaRenderer" includes="commons-logging-1.1.1.jar"/>
24-
<zipfileset dir="C:\workspace\CLIMediaRenderer" includes="javampd-4.1.jar"/>
24+
<zipfileset dir="C:\workspace\CLIMediaRenderer" includes="JMPDComm.jar"/>
2525
</jar>
2626
</target>
2727
</project>

cliMediaRenderer.jar

-282 KB
Binary file not shown.

javampd-4.1.jar

-76.5 KB
Binary file not shown.

src/org/matt1/climediarenderer/MediaRenderer.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ public static void main(final String[] args) throws Exception {
2020

2121
configureLogging();
2222

23-
CliMediaRenderer mediaRenderer = new CliMediaRenderer(PropertyHelper.getName());
24-
25-
UpnpServiceImpl upnp = new UpnpServiceImpl(new ApacheServiceConfiguration());
26-
27-
upnp.getRegistry().addDevice(
28-
mediaRenderer.getDevice()
29-
);
23+
try {
24+
CliMediaRenderer mediaRenderer = new CliMediaRenderer(PropertyHelper.getInstance().getName());
25+
26+
UpnpServiceImpl upnp = new UpnpServiceImpl(new ApacheServiceConfiguration());
27+
28+
upnp.getRegistry().addDevice(
29+
mediaRenderer.getDevice()
30+
);
31+
} catch (Exception e) {
32+
Logger.getLogger(MediaRenderer.class.getName()).severe("Unexpected error starting up: " + e.getMessage());
33+
}
3034

3135
}
3236

src/org/matt1/climediarenderer/player/MPlayer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ public static void main(String[] args) throws PlayerException, InterruptedExcept
5555
*/
5656
public MPlayer(String mediaPath) throws PlayerException {
5757
try {
58-
mPlayer = Runtime.getRuntime().exec(PropertyHelper.getMPlayerPath() + " -slave -quiet -idle ");
58+
mPlayer = Runtime.getRuntime().exec(PropertyHelper.getInstance().getMPlayerPath() + " -slave -quiet -idle ");
5959
mPlayerStream = new PrintStream(mPlayer.getOutputStream());
6060
this.mediaPath = mediaPath;
6161
Runtime.getRuntime().addShutdownHook(cleanupThread);
6262
log.info("New MPlayer player ready.");
6363
} catch (IOException e) {
6464
log.warning("Unable to create MPlayer instance.");
6565
throw new PlayerException("Unable to instantiate player.");
66+
} catch (Exception e) {
67+
log.severe(e.getMessage());
6668
}
6769
}
6870

src/org/matt1/climediarenderer/player/PlayerFactory.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ public class PlayerFactory {
1616
* @throws PlayerException
1717
*/
1818
public static BasicPlayer getPlayer(String mediaPath) throws PlayerException {
19-
20-
String type = PropertyHelper.getMediaPlayer().toLowerCase();
19+
String type;
20+
try {
21+
type = PropertyHelper.getInstance().getMediaPlayer().toLowerCase();
22+
} catch (Exception e) {
23+
throw new PlayerException("Unable to load media player type from config: " + e.getMessage());
24+
}
2125

2226
if (type.equals("mplayer")) {
2327
return new MPlayer(mediaPath);
2428
} else {
2529
throw new PlayerException("Unknown media player type");
2630
}
2731

28-
2932

3033
}
3134

src/org/matt1/climediarenderer/services/CliMediaRenderer.java

+29-7
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,24 @@ public class CliMediaRenderer {
7979
/** Longer lock for slower hardware/JRE */
8080
protected static final int TIMEOUT = 2000;
8181

82+
/** PropertyHelper instance */
83+
protected PropertyHelper properties;
84+
8285
/**
8386
* Creates a new device using the default device details
8487
*
8588
* @throws IllegalArgumentException
8689
* @throws ValidationException
8790
* @throws IOException
8891
*/
89-
public CliMediaRenderer() throws IllegalArgumentException, ValidationException, IOException {
90-
92+
public CliMediaRenderer() throws IllegalArgumentException, ValidationException, IOException, Exception {
93+
9194
this(new DeviceDetails(
9295
"CliMediaRenderer",
9396
new ManufacturerDetails(MANUFACTURER_NAME, MANUFACTURER_SITE),
9497
new ModelDetails(MODEL_NAME, MODEL_DESCRIPTION, MODEL_VERSION, MODEL_SITE)
9598
));
99+
96100
}
97101

98102
/**
@@ -104,28 +108,44 @@ public CliMediaRenderer() throws IllegalArgumentException, ValidationException,
104108
* @throws IOException
105109
*/
106110
public CliMediaRenderer(String name) throws IllegalArgumentException,
107-
ValidationException, IOException {
111+
ValidationException, IOException, Exception {
108112
this(new DeviceDetails(
109113
name,
110114
new ManufacturerDetails(MANUFACTURER_NAME, MANUFACTURER_SITE),
111115
new ModelDetails(MODEL_NAME, MODEL_DESCRIPTION, MODEL_VERSION, MODEL_SITE)
112116
));
113117
}
114118

119+
/**
120+
* Try loading the properties from the configuration file.
121+
*
122+
* @throws Exception
123+
*/
124+
private void loadProperties() throws Exception {
125+
126+
// Load properties
127+
try {
128+
properties = PropertyHelper.getInstance();
129+
} catch (Exception e) {
130+
log.severe("Unable to load the config file! " + e.getMessage());
131+
System.exit(1);
132+
}
133+
}
134+
115135
/**
116136
* Try loading the icon specified in the properties file
117137
*/
118138
private Icon loadIcon() {
119-
File iconFile = new File(PropertyHelper.getIconPath());
139+
File iconFile = new File(properties.getIconPath());
120140
Icon icon = null;
121141
try {
122142
if (iconFile.exists() && iconFile.canRead()) {
123143
icon = new Icon("image/png", 48, 48, 8, iconFile);
124144
} else {
125-
log.warning("Custom icon " + PropertyHelper.getIconPath() + " could not be read.");
145+
log.warning("Custom icon " + properties.getIconPath() + " could not be read.");
126146
}
127147
} catch (IOException e) {
128-
log.warning("IO Exception trying to load icon file at " + PropertyHelper.getIconPath());
148+
log.warning("IO Exception trying to load icon file at " + properties.getIconPath());
129149
}
130150
return icon;
131151
}
@@ -139,8 +159,10 @@ private Icon loadIcon() {
139159
*/
140160
@SuppressWarnings("unchecked")
141161
public CliMediaRenderer(DeviceDetails deviceDetails) throws IllegalArgumentException,
142-
ValidationException, IOException {
162+
ValidationException, IOException, Exception {
143163

164+
loadProperties();
165+
144166
LocalService<CliMRConnectionManagerService> connectionManagerService = serviceBinder.read(CliMRConnectionManagerService.class);
145167
connectionServiceManager =
146168
new DefaultServiceManager<CliMRConnectionManagerService>(connectionManagerService) {

src/org/matt1/climediarenderer/utils/PropertyHelper.java

+29-16
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,77 @@
1212
*/
1313
public class PropertyHelper {
1414

15-
private static Properties properties = new Properties();
16-
static {
17-
try {
18-
properties.load(new FileInputStream("cliMediaRenderer.properties"));
19-
} catch (FileNotFoundException e) {
20-
// TODO Auto-generated catch block
21-
e.printStackTrace();
22-
} catch (IOException e) {
23-
// TODO Auto-generated catch block
24-
e.printStackTrace();
15+
private Properties properties;
16+
private static PropertyHelper propertyHelper;
17+
18+
19+
public static PropertyHelper getInstance() throws Exception {
20+
21+
if (propertyHelper == null) {
22+
23+
propertyHelper = new PropertyHelper();
24+
25+
try {
26+
propertyHelper.properties.load(new FileInputStream("cliMediaRenderer.properties"));
27+
} catch (FileNotFoundException e) {
28+
throw new Exception("File not found: ./cliMediaRenderer.properties", e);
29+
} catch (IOException e) {
30+
throw new Exception("Cannot open file: ./cliMediaRenderer.properties", e);
31+
}
2532
}
33+
return propertyHelper;
34+
}
35+
36+
private PropertyHelper() {
37+
properties = new Properties();
38+
2639
}
2740

2841
/**
2942
* Gets the playerType property for use by the player factory
3043
* @return
3144
*/
32-
public static String getMediaPlayer() {
45+
public String getMediaPlayer() {
3346
return (String) properties.get("playerType");
3447
}
3548

3649
/**
3750
* Gets the path to MPlayer
3851
* @return
3952
*/
40-
public static String getMPlayerPath() {
53+
public String getMPlayerPath() {
4154
return (String) properties.get("mplayerPath");
4255
}
4356

4457
/**
4558
* Gets the name of the player
4659
* @return
4760
*/
48-
public static String getName() {
61+
public String getName() {
4962
return (String) properties.getProperty("name");
5063
}
5164

5265
/**
5366
* Gets the path to the icon file
5467
* @return
5568
*/
56-
public static String getIconPath() {
69+
public String getIconPath() {
5770
return (String) properties.getProperty("icon");
5871
}
5972

6073
/**
6174
* Gets the hostname of the MPD client
6275
* @return
6376
*/
64-
public static String getMPDHost() {
77+
public String getMPDHost() {
6578
return (String) properties.getProperty("mpdHost");
6679
}
6780

6881
/**
6982
* Gets the port of the MPD client
7083
* @return
7184
*/
72-
public static int getMPDPort() {
85+
public int getMPDPort() {
7386
return Integer.valueOf(properties.getProperty("mpdPort"));
7487
}
7588
}

0 commit comments

Comments
 (0)