Skip to content

Commit 2258d6b

Browse files
author
Michael Spector
committed
Fixed resolving classes
1 parent d646fdc commit 2258d6b

File tree

8 files changed

+311
-452
lines changed

8 files changed

+311
-452
lines changed
222 Bytes
Binary file not shown.

examples/addressbook/decode_addressbook.ktr

Lines changed: 186 additions & 394 deletions
Large diffs are not rendered by default.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
4+
<classpathentry kind="output" path="target/classes"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="var" path="M2_REPO/com/google/protobuf/protobuf-java/2.5.0/protobuf-java-2.5.0.jar" sourcepath="M2_REPO/com/google/protobuf/protobuf-java/2.5.0/protobuf-java-2.5.0-sources.jar"/>
7+
</classpath>

examples/addressbook/java/.project

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>protobuf-addressbook-example</name>
4+
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
5+
<projects/>
6+
<buildSpec>
7+
<buildCommand>
8+
<name>org.eclipse.jdt.core.javabuilder</name>
9+
</buildCommand>
10+
</buildSpec>
11+
<natures>
12+
<nature>org.eclipse.jdt.core.javanature</nature>
13+
</natures>
14+
</projectDescription>

src/main/java/com/ruckuswireless/pentaho/protobuf/decode/ProtobufDecodeDialog.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ public void widgetSelected(SelectionEvent e) {
293293
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
294294
dialog.setFilterExtensions(new String[] { "*.jar", "*" });
295295
if (wClasspath.getText() != null) {
296-
String fname = transMeta.environmentSubstitute(wClasspath
297-
.getText());
296+
String fname = wClasspath.getText(); // transMeta.environmentSubstitute(wClasspath.getText());
298297
dialog.setFileName(fname);
299298
}
300299

@@ -382,10 +381,9 @@ private void cancel() {
382381
* Copy information from the dialog fields to the meta-data input
383382
*/
384383
private void setData(ProtobufDecodeMeta consumerMeta) {
385-
consumerMeta.setInputField(transMeta.environmentSubstitute(wInputField
386-
.getText()));
387-
consumerMeta.setClasspath(transMeta.environmentSubstitute(
388-
wClasspath.getText().trim()).split(File.pathSeparator));
384+
consumerMeta.setInputField(wInputField.getText());
385+
consumerMeta.setClasspath(wClasspath.getText().trim()
386+
.split(File.pathSeparator));
389387
consumerMeta.setRootClass(wRootClass.getText());
390388

391389
int nrNonEmptyFields = wFields.nrNonEmpty();
@@ -417,28 +415,33 @@ private void ok() {
417415

418416
private void detectFields() {
419417
try {
420-
ProtobufDecoder protobufDecoder = new ProtobufDecoder(transMeta
421-
.environmentSubstitute(wClasspath.getText().trim()).split(
422-
File.pathSeparator), wRootClass.getText(), null);
423-
Map<String, Class<?>> fields = protobufDecoder.guessFields();
424-
RowMeta rowMeta = new RowMeta();
425-
for (Entry<String, Class<?>> e : fields.entrySet()) {
426-
String fieldPath = e.getKey();
427-
int i = fieldPath.lastIndexOf('.');
428-
String fieldName = i != -1 ? fieldPath.substring(i + 1)
429-
: fieldPath;
430-
rowMeta.addValueMeta(new FieldMeta(fieldName, fieldPath,
431-
KettleTypesConverter.javaToKettleType(e.getValue())));
418+
ProtobufDecoder protobufDecoder = new ProtobufDecoder(
419+
transMeta.environmentSubstitute(wClasspath.getText().trim()
420+
.split(File.pathSeparator)), wRootClass.getText(),
421+
null);
422+
try {
423+
Map<String, Class<?>> fields = protobufDecoder.guessFields();
424+
RowMeta rowMeta = new RowMeta();
425+
for (Entry<String, Class<?>> e : fields.entrySet()) {
426+
String fieldPath = e.getKey();
427+
int i = fieldPath.lastIndexOf('.');
428+
String fieldName = i != -1 ? fieldPath.substring(i + 1)
429+
: fieldPath;
430+
rowMeta.addValueMeta(new FieldMeta(fieldName, fieldPath,
431+
KettleTypesConverter.javaToKettleType(e.getValue())));
432+
}
433+
BaseStepDialog.getFieldsFromPrevious(rowMeta, wFields, 1,
434+
new int[] { 1 }, new int[] { 3 }, -1, -1,
435+
new TableItemInsertListener() {
436+
public boolean tableItemInserted(
437+
TableItem tableItem, ValueMetaInterface v) {
438+
tableItem.setText(2, ((FieldMeta) v).path);
439+
return true;
440+
}
441+
});
442+
} finally {
443+
protobufDecoder.dispose();
432444
}
433-
BaseStepDialog.getFieldsFromPrevious(rowMeta, wFields, 1,
434-
new int[] { 1 }, new int[] { 3 }, -1, -1,
435-
new TableItemInsertListener() {
436-
public boolean tableItemInserted(TableItem tableItem,
437-
ValueMetaInterface v) {
438-
tableItem.setText(2, ((FieldMeta) v).path);
439-
return true;
440-
}
441-
});
442445
} catch (ProtobufDecoderException e) {
443446
new ErrorDialog(
444447
shell,

src/main/java/com/ruckuswireless/pentaho/protobuf/decode/ProtobufDecodeStep.java

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.pentaho.di.core.Const;
66
import org.pentaho.di.core.exception.KettleException;
77
import org.pentaho.di.core.row.RowDataUtil;
8-
import org.pentaho.di.core.row.RowMetaInterface;
98
import org.pentaho.di.trans.Trans;
109
import org.pentaho.di.trans.TransMeta;
1110
import org.pentaho.di.trans.step.BaseStep;
@@ -23,8 +22,9 @@
2322
*/
2423
public class ProtobufDecodeStep extends BaseStep implements StepInterface {
2524

26-
public ProtobufDecodeStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
27-
Trans trans) {
25+
public ProtobufDecodeStep(StepMeta stepMeta,
26+
StepDataInterface stepDataInterface, int copyNr,
27+
TransMeta transMeta, Trans trans) {
2828
super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
2929
}
3030

@@ -34,15 +34,34 @@ public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
3434
ProtobufDecodeMeta meta = (ProtobufDecodeMeta) smi;
3535
ProtobufDecodeData data = (ProtobufDecodeData) sdi;
3636
try {
37-
data.decoder = new ProtobufDecoder(meta.getClasspath(), meta.getRootClass(), meta.getFields());
37+
data.decoder = new ProtobufDecoder(
38+
environmentSubstitute(meta.getClasspath()),
39+
meta.getRootClass(), meta.getFields());
3840
} catch (ProtobufDecoderException e) {
39-
logError(Messages.getString("ProtobufDecodeStep.Init.Error", getStepname()), e);
41+
logError(Messages.getString("ProtobufDecodeStep.Dispose.Error",
42+
getStepname()), e);
4043
return false;
4144
}
4245
return true;
4346
}
4447

45-
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
48+
public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
49+
50+
ProtobufDecodeData data = (ProtobufDecodeData) sdi;
51+
if (data.decoder != null) {
52+
try {
53+
data.decoder.dispose();
54+
} catch (ProtobufDecoderException e) {
55+
logError(Messages.getString("ProtobufDecodeStep.Init.Error",
56+
getStepname()), e);
57+
}
58+
data.decoder = null;
59+
}
60+
super.dispose(smi, sdi);
61+
}
62+
63+
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi)
64+
throws KettleException {
4665
Object[] r = getRow();
4766
if (r == null) {
4867
setOutputDone();
@@ -52,66 +71,74 @@ public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws K
5271
ProtobufDecodeMeta meta = (ProtobufDecodeMeta) smi;
5372
ProtobufDecodeData data = (ProtobufDecodeData) sdi;
5473

55-
RowMetaInterface inputRowMeta = getInputRowMeta();
56-
5774
if (first) {
5875
first = false;
59-
data.outputRowMeta = inputRowMeta.clone();
76+
data.outputRowMeta = getInputRowMeta().clone();
6077
meta.getFields(data.outputRowMeta, getStepname(), null, null, this);
6178

6279
String inputField = environmentSubstitute(meta.getInputField());
6380

6481
int numErrors = 0;
6582
if (Const.isEmpty(inputField)) {
66-
logError(Messages.getString("ProtobufDecodeStep.Log.FieldNameIsNull")); //$NON-NLS-1$
83+
logError(Messages
84+
.getString("ProtobufDecodeStep.Log.FieldNameIsNull")); //$NON-NLS-1$
6785
numErrors++;
6886
}
69-
data.inputFieldNr = inputRowMeta.indexOfValue(inputField);
87+
data.inputFieldNr = getInputRowMeta().indexOfValue(inputField);
7088
if (data.inputFieldNr < 0) {
71-
logError(Messages.getString("ProtobufDecodeStep.Log.CouldntFindField", inputField)); //$NON-NLS-1$
89+
logError(Messages.getString(
90+
"ProtobufDecodeStep.Log.CouldntFindField", inputField)); //$NON-NLS-1$
7291
numErrors++;
7392
}
74-
if (!inputRowMeta.getValueMeta(data.inputFieldNr).isBinary()) {
75-
logError(Messages.getString("ProtobufDecodeStep.Log.FieldNotValid", inputField)); //$NON-NLS-1$
93+
if (!getInputRowMeta().getValueMeta(data.inputFieldNr).isBinary()) {
94+
logError(Messages.getString(
95+
"ProtobufDecodeStep.Log.FieldNotValid", inputField)); //$NON-NLS-1$
7696
numErrors++;
7797
}
7898
if (numErrors > 0) {
7999
setErrors(numErrors);
80100
stopAll();
81101
return false;
82102
}
83-
data.inputFieldMeta = inputRowMeta.getValueMeta(data.inputFieldNr);
103+
data.inputFieldMeta = getInputRowMeta().getValueMeta(
104+
data.inputFieldNr);
84105
}
85106

86107
try {
87-
byte[] message = data.inputFieldMeta.getBinary(r[data.inputFieldNr]);
108+
byte[] message = data.inputFieldMeta
109+
.getBinary(r[data.inputFieldNr]);
88110
try {
89111
List<Object[]> decodedData = data.decoder.decode(message);
90112
for (Object[] d : decodedData) {
91-
r = RowDataUtil.addRowData(r, inputRowMeta.size(), d);
113+
r = RowDataUtil.addRowData(r, getInputRowMeta().size(), d);
92114
putRow(data.outputRowMeta, r);
93115
if (isRowLevel()) {
94-
logRowlevel(Messages.getString("ProtobufDecodeStep.Log.OutputRow",
95-
Long.toString(getLinesWritten()), data.outputRowMeta.getString(r)));
116+
logRowlevel(Messages.getString(
117+
"ProtobufDecodeStep.Log.OutputRow",
118+
Long.toString(getLinesWritten()),
119+
data.outputRowMeta.getString(r)));
96120
}
97121
}
98122
} catch (ProtobufDecoderException e) {
99123
throw new KettleException(e);
100124
}
101125
} catch (KettleException e) {
102126
if (!getStepMeta().isDoingErrorHandling()) {
103-
logError(Messages.getString("ProtobufDecodeStep.ErrorInStepRunning", e.getMessage()));
127+
logError(Messages
128+
.getString("ProtobufDecodeStep.ErrorInStepRunning",
129+
e.getMessage()));
104130
setErrors(1);
105131
stopAll();
106132
setOutputDone();
107133
return false;
108134
}
109-
putError(inputRowMeta, r, 1, e.toString(), null, getStepname());
135+
putError(getInputRowMeta(), r, 1, e.toString(), null, getStepname());
110136
}
111137
return true;
112138
}
113139

114-
public void stopRunning(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
140+
public void stopRunning(StepMetaInterface smi, StepDataInterface sdi)
141+
throws KettleException {
115142

116143
ProtobufDecodeData data = (ProtobufDecodeData) sdi;
117144
data.canceled = true;

src/main/java/com/ruckuswireless/pentaho/protobuf/decode/ProtobufDecoder.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
public class ProtobufDecoder {
2929

3030
private final Object EMPTY = new Object();
31+
private URLClassLoader classLoader;
3132
private Class<?> rootClass;
3233
private Method rootParseFromMethod;
3334
private LinkedHashMap<String, Integer> paths;
@@ -36,13 +37,17 @@ public class ProtobufDecoder {
3637
public ProtobufDecoder(String[] classpath, String rootClass,
3738
FieldDefinition[] fields) throws ProtobufDecoderException {
3839

39-
URLClassLoader classLoader;
4040
try {
4141
URL[] url = new URL[classpath.length];
4242
for (int i = 0; i < classpath.length; ++i) {
43-
url[i] = new File(classpath[i]).toURI().toURL();
43+
String file = classpath[i];
44+
if (file.startsWith("file://")) {
45+
file = file.substring(7);
46+
}
47+
url[i] = new File(file).toURI().toURL();
4448
}
45-
classLoader = new URLClassLoader(url, getClass().getClassLoader());
49+
this.classLoader = new URLClassLoader(url, getClass()
50+
.getClassLoader());
4651
} catch (MalformedURLException e) {
4752
throw new ProtobufDecoderException(e);
4853
}
@@ -51,12 +56,6 @@ public ProtobufDecoder(String[] classpath, String rootClass,
5156
this.rootClass = classLoader.loadClass(rootClass);
5257
} catch (ClassNotFoundException e) {
5358
throw new ProtobufDecoderException("Can't find root class", e);
54-
} finally {
55-
try {
56-
classLoader.close();
57-
} catch (IOException e) {
58-
throw new ProtobufDecoderException(e);
59-
}
6059
}
6160

6261
try {
@@ -78,6 +77,22 @@ public ProtobufDecoder(String[] classpath, String rootClass,
7877
}
7978
}
8079

80+
/**
81+
* Disposes the decoder
82+
*
83+
* @throws ProtobufDecoderException
84+
*/
85+
public void dispose() throws ProtobufDecoderException {
86+
if (classLoader != null) {
87+
try {
88+
classLoader.close();
89+
} catch (IOException e) {
90+
throw new ProtobufDecoderException(e);
91+
}
92+
classLoader = null;
93+
}
94+
}
95+
8196
/**
8297
* Decodes message, and returns denormalized rows for the object fields
8398
* exactly in the order they appear in <code>fields</code> parameter.

src/main/resources/com/ruckuswireless/pentaho/protobuf/decode/messages/messages_en_US.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ProtobufDecodeStep.Init.Error=Error initializing step\: {0}
2+
ProtobufDecodeStep.Dispose.Error=Error disposing step\: {0}
23
ProtobufDecodeStep.Log.OutputRow=Outputting row {0} \: {1}
34
ProtobufDecodeStep.Log.FieldNameIsNull=Input field name not specified\!
45
ProtobufDecodeStep.Log.CouldntFindField=Couldn''t find field ''{0}'' in input stream\!

0 commit comments

Comments
 (0)