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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
|
// **********************************************************************
//
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
//
// This plug-in is provided to you under the terms and conditions
// of the Eclipse Public License Version 1.0 ("EPL"). A copy of
// the EPL is available at http://www.eclipse.org/legal/epl-v10.html.
//
// **********************************************************************
package com.zeroc.slice2javaplugin.internal;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import com.zeroc.slice2javaplugin.Activator;
import com.zeroc.slice2javaplugin.preferences.PluginPreferencePage;
public class Configuration
{
public Configuration(IProject project)
{
_project = project;
_instanceStore = new ScopedPreferenceStore(new InstanceScope(), Activator.PLUGIN_ID + "." + _project.getName());
_store = new ScopedPreferenceStore(new ProjectScope(project), Activator.PLUGIN_ID);
_store.setDefault(GENERATED_KEY, GENERATED_KEY);
_store.setDefault(DEFINES_KEY, "");
_store.setDefault(TIE_KEY, false);
_store.setDefault(ICE_KEY, false);
_store.setDefault(STREAM_KEY, false);
_store.setDefault(ICE_INCLUDE_KEY, false);
_store.setDefault(META_KEY, "");
_store.setDefault(CONSOLE_KEY, false);
_store.setDefault(SLICE_SOURCE_DIRS_KEY, "slice");
_store.setDefault(INCLUDES_KEY, "");
boolean androidProject = false;
try
{
androidProject = project.hasNature("com.android.ide.eclipse.adt.AndroidNature");
}
catch(CoreException e)
{
}
if(androidProject)
{
// TODO: At present android does not work with indirect libraries.
//_store.setDefault(JARS_KEY, "IceAndroid.jar");
_store.setDefault(JARS_KEY, "");
}
else
{
_store.setDefault(JARS_KEY, "Ice.jar");
}
}
/**
* Turns list of strings into a single ';' delimited string. ';' in the
* string values are escaped with a leading '\'. '\' are turned into '\\'.
*
* @param l
* List of strings.
* @return Semicolon delimited string.
*/
static public String fromList(List<String> l)
{
StringBuffer sb = new StringBuffer();
for(Iterator<String> p = l.iterator(); p.hasNext();)
{
if(sb.length() > 0)
{
sb.append(";");
}
sb.append(escape(p.next()));
}
return sb.toString();
}
/**
* Turn a semicolon delimited string into a list of strings. Escaped values
* are preserved (characters prefixed with a '\').
*
* @param s
* Semicolon delimited string.
* @return List of strings.
*/
static public List<String> toList(String s)
{
java.util.List<String> l = new ArrayList<String>();
int curr = 0;
int end = s.length();
boolean escape = false;
StringBuffer sb = new StringBuffer();
for(curr = 0; curr < end; ++curr)
{
char ch = s.charAt(curr);
if(escape)
{
sb.append(ch);
escape = false;
}
else
{
if(ch == ';')
{
String tok = sb.toString().trim();
sb.setLength(0);
if(tok.length() > 0)
{
l.add(tok);
}
}
else if(ch == '\\')
{
escape = true;
}
else
{
sb.append(ch);
}
}
}
String tok = sb.toString().trim();
if(tok.length() > 0)
{
l.add(tok);
}
return l;
}
public boolean write()
throws CoreException, IOException
{
boolean rc = false;
if(_store.needsSaving())
{
_store.save();
rc = true;
}
if(_instanceStore.needsSaving())
{
_instanceStore.save();
rc = true;
}
return rc;
}
public void initialize()
throws CoreException
{
// Create the slice source directories, if necessary.
for(Iterator<String> p = getSliceSourceDirs().iterator(); p.hasNext();)
{
IFolder slice = _project.getFolder(p.next());
if(!slice.exists())
{
slice.create(false, true, null);
}
}
// Create the generated directory, if necessary.
IFolder generated = _project.getFolder(getGeneratedDir());
if(!generated.exists())
{
generated.create(false, true, null);
}
fixGeneratedCP(null, getGeneratedDir());
IJavaProject javaProject = JavaCore.create(_project);
IClasspathEntry cpEntry = IceClasspathContainerIntializer.getContainerEntry();
IClasspathEntry[] entries = javaProject.getRawClasspath();
boolean found = false;
for(int i = 0; i < entries.length; ++i)
{
if(entries[i].equals(cpEntry))
{
found = true;
break;
}
}
if(!found)
{
IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
System.arraycopy(entries, 0, newEntries, 0, entries.length);
newEntries[entries.length] = cpEntry;
try
{
javaProject.setRawClasspath(newEntries, null);
}
catch(JavaModelException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), null));
}
}
}
public void deinstall()
throws CoreException
{
IJavaProject javaProject = JavaCore.create(_project);
IClasspathEntry cpEntry = IceClasspathContainerIntializer.getContainerEntry();
IClasspathEntry[] entries = javaProject.getRawClasspath();
for(int i = 0; i < entries.length; ++i)
{
if(entries[i].equals(cpEntry))
{
IClasspathEntry[] newEntries = new IClasspathEntry[entries.length - 1];
System.arraycopy(entries, 0, newEntries, 0, i);
System.arraycopy(entries, i + 1, newEntries, i, entries.length - i - 1);
try
{
javaProject.setRawClasspath(newEntries, null);
}
catch(JavaModelException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), null));
}
break;
}
}
}
public List<String> getSliceSourceDirs()
{
return toList(_store.getString(SLICE_SOURCE_DIRS_KEY));
}
public void setSliceSourceDirs(List<String> sliceSourceDirs)
{
setValue(SLICE_SOURCE_DIRS_KEY, fromList(sliceSourceDirs));
}
public String getGeneratedDir()
{
return _store.getString(GENERATED_KEY);
}
public void fixGeneratedCP(String oldG, String newG)
throws CoreException
{
IJavaProject javaProject = JavaCore.create(_project);
IFolder newGenerated = _project.getFolder(newG);
IClasspathEntry[] entries = javaProject.getRawClasspath();
IClasspathEntry newEntry = JavaCore.newSourceEntry(newGenerated.getFullPath());
if(oldG != null)
{
IFolder oldGenerated = _project.getFolder(oldG);
IClasspathEntry oldEntry = JavaCore.newSourceEntry(oldGenerated.getFullPath());
for(int i = 0; i < entries.length; ++i)
{
if(entries[i].equals(oldEntry))
{
entries[i] = newEntry;
javaProject.setRawClasspath(entries, null);
return;
}
}
}
IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
System.arraycopy(entries, 0, newEntries, 1, entries.length);
newEntries[0] = newEntry;
try
{
javaProject.setRawClasspath(newEntries, null);
}
catch(JavaModelException e)
{
// This can occur if a duplicate CLASSPATH entry is made.
//
// throw new CoreException(new Status(IStatus.ERROR,
// Activator.PLUGIN_ID, e.toString(), null));
}
}
public void setGeneratedDir(String generated)
throws CoreException
{
String oldGenerated = getGeneratedDir();
if(setValue(GENERATED_KEY, generated))
{
fixGeneratedCP(oldGenerated, generated);
}
}
public List<String> getCommandLine()
{
List<String> cmds = new ArrayList<String>();
for(Iterator<String> p = getIncludes().iterator(); p.hasNext();)
{
cmds.add("-I" + p.next());
}
for(Iterator<String> p = getDefines().iterator(); p.hasNext();)
{
cmds.add("-D" + p.next());
}
for(Iterator<String> p = getMeta().iterator(); p.hasNext();)
{
cmds.add("--meta");
cmds.add(p.next());
}
if(getStream())
{
cmds.add("--stream");
}
if(getTie())
{
cmds.add("--tie");
}
if(getIce())
{
cmds.add("--ice");
}
return cmds;
}
public boolean getIceInclude()
{
return _store.getBoolean(ICE_INCLUDE_KEY);
}
public void setIceInclude(boolean selection)
{
_store.setValue(ICE_INCLUDE_KEY, selection);
}
public List<String> getIncludes()
{
List<String> s = toList(_store.getString(INCLUDES_KEY));
if(getIceInclude())
{
String iceHome = getIceHome();
String os = System.getProperty("os.name");
String path = null;
if(os.equals("Linux") && iceHome.equals("/usr"))
{
String version = getIceVersion();
if(version != null)
{
File f = new File("/usr/share/Ice-" + version + "/slice");
if(f.exists())
{
path = f.toString();
}
}
}
if(path == null)
{
path = new File(iceHome + File.separator + "slice").toString();
}
s.add(path);
}
return s;
}
// The bare include list.
public List<String> getBareIncludes()
{
return toList(_store.getString(INCLUDES_KEY));
}
public void setIncludes(List<String> includes)
{
setValue(INCLUDES_KEY, fromList(includes));
}
public List<String> getJars()
{
return toList(_store.getString(JARS_KEY));
}
public void setJars(List<String> jars) throws CoreException
{
if(setValue(JARS_KEY, fromList(jars)))
{
IceClasspathContainerIntializer.reinitialize(_project, this);
}
}
public List<String> getDefines()
{
return toList(_store.getString(DEFINES_KEY));
}
public void setDefines(List<String> defines)
{
setValue(DEFINES_KEY, fromList(defines));
}
public boolean getStream()
{
return _store.getBoolean(STREAM_KEY);
}
public void setStream(boolean stream)
{
_store.setValue(STREAM_KEY, stream);
}
public boolean getTie()
{
return _store.getBoolean(TIE_KEY);
}
public void setTie(boolean tie)
{
_store.setValue(TIE_KEY, tie);
}
public boolean getIce()
{
return _store.getBoolean(ICE_KEY);
}
public void setIce(boolean ice)
{
_store.setValue(ICE_KEY, ice);
}
public boolean getConsole()
{
return _store.getBoolean(CONSOLE_KEY);
}
public void setConsole(boolean console)
{
_store.setValue(CONSOLE_KEY, console);
}
public List<String> getMeta()
{
return toList(_store.getString(META_KEY));
}
public void setMeta(List<String> meta)
{
setValue(META_KEY, fromList(meta));
}
public void setupSharedLibraryPath(Map<String, String> env)
{
String iceHome = getIceHome();
String libPath;
boolean srcdist = false;
if(new File(iceHome + File.separator + "cpp" + File.separator + "bin").exists())
{
// iceHome points at a source distribution.
libPath = new File(iceHome + File.separator + "cpp" + File.separator + "lib").toString();
srcdist = true;
}
else
{
libPath = new File(iceHome + File.separator + "lib").toString();
}
String ldLibPathEnv = null;
String ldLib64PathEnv = null;
String lib64Path = null;
String os = System.getProperty("os.name");
if(os.equals("Mac OS X"))
{
ldLibPathEnv = "DYLD_LIBRARY_PATH";
}
else if(os.equals("AIX"))
{
ldLibPathEnv = "LIBPATH";
}
else if(os.equals("HP-UX"))
{
ldLibPathEnv = "SHLIB_PATH";
ldLib64PathEnv = "LD_LIBRARY_PATH";
if(srcdist)
{
lib64Path = libPath;
}
else
{
lib64Path = new File(iceHome + File.separator + "lib" + File.separator + "pa20_64").toString();
}
}
else if(os.startsWith("Windows"))
{
//
// No need to change the PATH environment variable on Windows, the
// DLLs should be found
// in the translator local directory.
//
// ldLibPathEnv = "PATH";
}
else if(os.equals("SunOS"))
{
ldLibPathEnv = "LD_LIBRARY_PATH";
ldLib64PathEnv = "LD_LIBRARY_PATH_64";
String arch = System.getProperty("os.arch");
if(srcdist)
{
lib64Path = libPath;
}
else if(arch.equals("x86"))
{
lib64Path = new File(iceHome + File.separator + "lib" + File.separator + "amd64").toString();
}
else
// Sparc
{
lib64Path = new File(iceHome + File.separator + "lib" + File.separator + "sparcv9").toString();
}
}
else
{
ldLibPathEnv = "LD_LIBRARY_PATH";
ldLib64PathEnv = "LD_LIBRARY_PATH";
if(srcdist)
{
lib64Path = libPath;
}
else
{
lib64Path = new File(iceHome + File.separator + "lib64").toString();
}
}
if(ldLibPathEnv != null)
{
if(ldLibPathEnv.equals(ldLib64PathEnv))
{
libPath = libPath + File.pathSeparator + lib64Path;
}
String envLibPath = env.get(ldLibPathEnv);
if(envLibPath != null)
{
libPath = libPath + File.pathSeparator + envLibPath;
}
env.put(ldLibPathEnv, libPath);
}
if(ldLib64PathEnv != null && !ldLib64PathEnv.equals(ldLibPathEnv))
{
String envLib64Path = env.get(ldLib64PathEnv);
if(envLib64Path != null)
{
lib64Path = lib64Path + File.pathSeparator + envLib64Path;
}
env.put(ldLib64PathEnv, lib64Path);
}
}
public String getTranslator()
{
return getTranslatorForHome(getIceHome());
}
static public boolean verifyIceHome(String dir)
{
return getTranslatorForHome(dir) != null;
}
public String getJarDir()
{
String iceHome = getIceHome();
String os = System.getProperty("os.name");
if(os.equals("Linux") && iceHome.equals("/usr"))
{
File f = new File(iceHome + File.separator + "share" + File.separator + "java");
if(f.exists())
{
return f.toString();
}
}
File f = new File(iceHome + File.separator + "lib");
if(!f.exists())
{
File f2 = new File(iceHome + File.separator + "java" + File.separator + "lib");
if(f2.exists())
{
return f2.toString();
}
}
// Add the platform default even if it cannot be found.
return f.toString();
}
private String getIceHome()
{
return Activator.getDefault().getPreferenceStore().getString(PluginPreferencePage.SDK_PATH);
}
// For some reason ScopedPreferenceStore.setValue(String, String)
// doesn't check to see whether the stored value is the same as
// the new value.
private boolean setValue(String key, String value)
{
return setValue(_store, key, value);
}
private boolean setValue(ScopedPreferenceStore store, String key, String value)
{
if(!store.getString(key).equals(value))
{
store.setValue(key, value);
return true;
}
return false;
}
static private String escape(String s)
{
int curr = 0;
int end = s.length();
StringBuffer sb = new StringBuffer();
for(curr = 0; curr < end; ++curr)
{
char ch = s.charAt(curr);
if(ch == '\\' || ch == ';')
{
sb.append('\\');
}
sb.append(ch);
}
return sb.toString();
}
// Obtain the Ice version by executing the translator with the -v option.
private String getIceVersion()
{
String version = null;
String exec = getTranslatorForHome(getIceHome());
if(exec != null)
{
try
{
ProcessBuilder b = new ProcessBuilder(exec, "-v");
b.redirectErrorStream(true);
Map<String, String> env = b.environment();
setupSharedLibraryPath(env);
Process p = b.start();
int status = p.waitFor();
if(status == 0)
{
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = r.readLine();
version = line.trim();
}
}
catch(Throwable ex)
{
// Ignore.
}
}
return version;
}
private static String getTranslatorForHome(String dir)
{
String suffix = "";
String os = System.getProperty("os.name");
if(os.startsWith("Windows"))
{
suffix = ".exe";
}
File f = new File(dir + File.separator + "bin" + File.separator + "slice2java" + suffix);
if(f.exists())
{
return f.toString();
}
f = new File(dir + File.separator + "cpp" + File.separator + "bin" + File.separator + "slice2java" + suffix);
if(f.exists())
{
return f.toString();
}
return null;
}
private static final String JARS_KEY = "jars";
private static final String INCLUDES_KEY = "includes";
private static final String SLICE_SOURCE_DIRS_KEY = "sliceSourceDirs";
private static final String CONSOLE_KEY = "console";
private static final String META_KEY = "meta";
private static final String STREAM_KEY = "stream";
private static final String ICE_INCLUDE_KEY = "iceIncludes";
private static final String ICE_KEY = "ice";
private static final String TIE_KEY = "tie";
private static final String DEFINES_KEY = "defines";
private static final String GENERATED_KEY = "generated";
// Preferences store for items which should go in SCM. This includes things
// like build flags.
private ScopedPreferenceStore _store;
// Preferences store per project items which should not go in SCM, such as
// the location of the Ice installation.
private ScopedPreferenceStore _instanceStore;
private IProject _project;
}
|