My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package net.riaspace.flerry
{
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.filesystem.File;
import flash.system.Capabilities;

import mx.utils.StringUtil;

import net.riaspace.flerry.events.FlerryInitEvent;

[Bindable]
public class BaseStartupInfoProvider extends EventDispatcher implements IStartupInfoProvider
{

protected var classpath:String = "./libs/flerry.jar";

public var libsDirectory:String;

public var source:String;

public var singleton:Boolean;

public var debug:Boolean;

public var debugPort:uint;

protected var findJavaProcess:NativeProcess;

private var os:String;

public function BaseStartupInfoProvider(libsDirectory:String = null, source:String = null, singleton:Boolean = false, debug:Boolean = false, debugPort:uint = 8000)
{
this.os = Capabilities.os.toLowerCase();

this.libsDirectory = libsDirectory;
this.source = source;
this.singleton = singleton;
this.debug = debug;
this.debugPort = debugPort;

processClasspath();
}

/**
* Process the $jarsDir, adding all jars to classpath
*/
protected function processClasspath():void
{
var separator:String = os.indexOf('win') > -1 ? ';':':';

var jarsDir:File = File.applicationDirectory.resolvePath(libsDirectory);
var jars:Array = jarsDir.getDirectoryListing();

classpath = classpath + separator;

for (var i:int = 0; i < jars.length; i++)
{
if(classpath.indexOf(File(jars[i]).name) != -1)
{
continue;
}

classpath += "./" + libsDirectory + "/" + File(jars[i]).name + separator;
}

classpath += "./classes";
}

public function findJava():void
{
var java:File;
// Chacking first in default locations
if (os.indexOf('win') > -1)
java = new File("c:/windows/system32/javaw.exe");
else
{
java = new File("/usr/bin/java");
if (!java.exists)
java = new File(os.indexOf("mac") > -1 ? "/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java" : "/etc/alternatives/java");
}

// If default locations succeeded
if (java.exists)
handleResultEvent(java);
// Running fallback mechanizm with native services
else
{
if (os.indexOf('win') > -1)
runFallbackMechanizm(File.applicationDirectory.resolvePath(libsDirectory).resolvePath("FindJava.exe"));
else
{
var args:Vector.<String> = new Vector.<String>();
args.push("java");
runFallbackMechanizm(new File("/usr/bin/whereis"), args);
}
}
}

protected function runFallbackMechanizm(executable:File, args:Vector.<String> = null):void
{
try
{
var findJavaInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
findJavaInfo.executable = executable;
findJavaInfo.workingDirectory = File.applicationDirectory;
findJavaInfo.arguments = args

findJavaProcess = new NativeProcess();
findJavaProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, findJavaProcess_outputDataHandler);
findJavaProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, findJavaProcess_errorDataHandler);

findJavaProcess.addEventListener(IOErrorEvent.STANDARD_INPUT_IO_ERROR, findJavaProcess_ioErrorHandler);
findJavaProcess.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, findJavaProcess_ioErrorHandler);
findJavaProcess.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, findJavaProcess_ioErrorHandler);

findJavaProcess.start(findJavaInfo);
}
catch (error:Error)
{
handleError("Couldn't execute whereis java: " + error.message);
}
}

private function findJavaProcess_ioErrorHandler(event:IOErrorEvent):void
{
handleError("Error finding Java: " + event.text);
}

private function findJavaProcess_errorDataHandler(event:ProgressEvent):void
{
handleError("Error finding Java: " + event.type);
}

private function findJavaProcess_outputDataHandler(event:ProgressEvent):void
{
var java:File = new File(StringUtil.trim(findJavaProcess.standardOutput.readUTFBytes(findJavaProcess.standardOutput.bytesAvailable)));
if (os.indexOf('win') > -1) // In case of win FindJava.exe returns only path to directory with java
java = java.resolvePath("bin").resolvePath("javaw.exe");

handleResultEvent(java);

if (findJavaProcess.running)
findJavaProcess.exit();
findJavaProcess = null;
}

protected function handleResultEvent(java:File):void
{
if (java.exists)
dispatchEvent(new FlerryInitEvent(FlerryInitEvent.INIT_COMPLETE, createStartupInfo(java)));
else
handleError("Couldn't find java!");
}

protected function createStartupInfo(java:File):NativeProcessStartupInfo
{
var startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
startupInfo.executable = java;
startupInfo.workingDirectory = File.applicationDirectory;

var args:Vector.<String> = new Vector.<String>();
if (debug)
args.push("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=n,suspend=n,quiet=y,address=" + debugPort.toString());
args.push("-cp");
args.push(classpath);
args.push("net.riaspace.flerry.NativeObject");
args.push("-source", source, "-singleton", singleton);
startupInfo.arguments = args;

return startupInfo;
}

protected function handleError(errorMessage:String):void
{
dispatchEvent(new FlerryInitEvent(FlerryInitEvent.INIT_ERROR, null, errorMessage));
}

}
}

Change log

r83 by piotr.walczyszyn on Jul 27, 2010   Diff
- cleaned fallback mechanizm invocation
Go to: 
Project members, sign in to write a code review

Older revisions

r82 by piotr.walczyszyn on Jul 27, 2010   Diff
- add fallback mechanizm for java
discovery for both win and unix
r80 by piotr.walczyszyn on Jul 27, 2010   Diff
- add checking of java location in
default location on windows platform
r76 by piotr.walczyszyn on Jul 27, 2010   Diff
- fixed the java path discovery on
windows
All revisions of this file

File info

Size: 5618 bytes, 183 lines
Powered by Google Project Hosting