How could I insert a statement before try statement in method using github.parser - javaparser

I am using Com.GitHub.java parser for generating java code. i am facing a problem . problem is :
I have need bellow output . This line "String result = null;" need to show before try statement .
But it is showing inside of try statement.
My Source code:
https://pastebin.mozilla.org/McXbnU7w
Maven dependency:
<!-- https://mvnrepository.com/artifact/com.github.javaparser/javaparser-core -->
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.24.4</version>
</dependency>
Expected output of my code:
public Response saveProduct(ProductDto productDto) throws ServletException, IOException {
String result = null;
try {
createMyProduct.setProduct(productDto.getProduct());
createMyProduct.execute(parameterMap);
result = createMyProduct.getResult();
} catch (BusinessException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(result).build();
}

You need to add as many blocks as you need. E.g. String result = null should be treated as a separate block and try block should be another block. You cannot shove all statements in one block. Also in addingException method, you are adding try block into another BlockStmt. That is not needed. It will put extra curly braces.
Here is updated code:
import java.io.IOException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.CatchClause;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.stmt.TryStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
public class ProductCreate2 {
public static void main(String[] args) {
CompilationUnit compilationUnit = new CompilationUnit();
compilationUnit.setPackageDeclaration("org.meveo.mymodule.resource");
compilationUnit.addImport("java.util", false, true);
ClassOrInterfaceDeclaration restEndpointClass = compilationUnit.addClass("ProductCreate",Modifier.Keyword.PUBLIC);
restEndpointClass.addField("CreateMyProduct", "createMyProduct", Modifier.Keyword.PRIVATE);
NodeList<ClassOrInterfaceType> extendsList = new NodeList<>();
extendsList.add(new ClassOrInterfaceType("CustomEndpointResource"));
restEndpointClass.setExtendedTypes(extendsList);
MethodDeclaration restMethod = restEndpointClass.addMethod("saveProduct", Modifier.Keyword.PUBLIC);
restMethod.addParameter("ProductDto", "productDto");
restMethod.setType("Response");
restMethod.addAndGetAnnotation("POST");
restMethod.addThrownException(IOException.class);
//restMethod.addThrownException(ServletException.class);
//restMethod.getBody().get().getStatements().add(new ExpressionStmt(new NameExpr("String result = null")));
//restMethod.createBody().addStatement(new ExpressionStmt(new NameExpr("String result = // null")));
BlockStmt blockStmt = new BlockStmt();
blockStmt.addStatement(new ExpressionStmt(new NameExpr("String result = null")));
BlockStmt blockStmt1 = new BlockStmt();
blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "setProduct").addArgument("productDto.getProduct()"));
blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "init").addArgument("parameterMap"));
blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "execute").addArgument("parameterMap"));
blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "finalize").addArgument("parameterMap"));
blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "getResult()"));
Statement blockStmt2 =addingException(blockStmt1);
blockStmt.addStatement(blockStmt2);
restMethod.setBody(blockStmt);
restMethod.getBody().get().getStatements().add(new ReturnStmt(new NameExpr("Response.status(Response.Status.OK).entity(result).build()")));
System.out.println(compilationUnit);
}
private static Statement addingException(BlockStmt body) {
TryStmt ts = new TryStmt();
ts.setTryBlock(body);
CatchClause cc = new CatchClause();
String exceptionName = "e";
cc.setParameter(new Parameter().setName(exceptionName).setType(Exception.class));
BlockStmt cb = new BlockStmt();
cb.addStatement(new ExpressionStmt(new NameExpr("return Response.status(Response.Status.BAD_REQUEST).entity(result).build()")));
// cb.addStatement(new ThrowStmt(new NameExpr(exceptionName)));
cc.setBody(cb);
ts.setCatchClauses(new NodeList<>(cc));
return ts;
}
}

Related

BigQuery Read Storage API with ARROW format appending 0s for NUMERIC and BIGNUMERIC data

I tried out the code in the Google document for Read Storage API implementation.
But the Numeric and BigNumeric columns are returning with appended 0s.
For example: my table have Numeric data 123, the below code returning it as below:
Schema<numeric_datatype: Decimal(38, 9, 128)>
numeric_datatype
123000000000.000000000
code used: https://cloud.google.com/bigquery/docs/reference/storage/libraries
Please help to understand and resolve the issue.
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.util.Preconditions;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.VectorLoader;
import org.apache.arrow.vector.ipc.ReadChannel;
import org.apache.arrow.vector.ipc.message.MessageSerializer;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.Schema;
import org.apache.arrow.vector.util.ByteArrayReadableSeekableByteChannel;
import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch;
import com.google.cloud.bigquery.storage.v1.ArrowSchema;
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest;
import com.google.cloud.bigquery.storage.v1.DataFormat;
import com.google.cloud.bigquery.storage.v1.ReadRowsRequest;
import com.google.cloud.bigquery.storage.v1.ReadRowsResponse;
import com.google.cloud.bigquery.storage.v1.ReadSession;
import com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers;
import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions;
import com.google.protobuf.Timestamp;
import com.google.api.gax.rpc.ServerStream;
public class StorageArrowExample {
private static class SimpleRowReader implements AutoCloseable {
BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
private final VectorSchemaRoot root;
private final VectorLoader loader;
public SimpleRowReader(ArrowSchema arrowSchema) throws IOException {
Schema schema = MessageSerializer.deserializeSchema(new ReadChannel(
new ByteArrayReadableSeekableByteChannel(arrowSchema.getSerializedSchema().toByteArray())));
System.out.println(schema);
Preconditions.checkNotNull(schema);
List<FieldVector> vectors = new ArrayList<>();
for (Field field : schema.getFields()) {
vectors.add(field.createVector(allocator));
}
root = new VectorSchemaRoot(vectors);
root.syncSchema();
loader = new VectorLoader(root);
}
public void processRows(ArrowRecordBatch batch) throws IOException {
org.apache.arrow.vector.ipc.message.ArrowRecordBatch deserializedBatch = MessageSerializer
.deserializeRecordBatch(new ReadChannel(
new ByteArrayReadableSeekableByteChannel(batch.getSerializedRecordBatch().toByteArray())),
allocator);
System.out.println(deserializedBatch);
loader.load(deserializedBatch);
// Release buffers from batch (they are still held in the vectors in root).
deserializedBatch.close();
String test = root.contentToTSVString();
System.out.println(root.contentToTSVString());
root.clear();
}
#Override
public void close() throws Exception {
// TODO Auto-generated method stub
}
}
public static void main(String... args) throws Exception {
String projectId = "****";
String table = "****";
String dataset = "****";
Integer snapshotMillis = null;
if (args.length > 1) {
snapshotMillis = Integer.parseInt(args[1]);
}
try (BigQueryReadClient client = BigQueryReadClient.create()) {
String parent = String.format("projects/%s", projectId);
String srcTable = String.format("projects/%s/datasets/%s/tables/%s", projectId, table, dataset);
TableReadOptions options = TableReadOptions.newBuilder().addSelectedFields("numeric_datatype")
.addSelectedFields("bignumeric_datatype").clearArrowSerializationOptions().build();
ReadSession.Builder sessionBuilder = ReadSession.newBuilder().setTable(srcTable)
.setDataFormat(DataFormat.ARROW).setReadOptions(options);
// Optionally specify the snapshot time. When unspecified, snapshot time is
// "now".
if (snapshotMillis != null) {
Timestamp t = Timestamp.newBuilder().setSeconds(snapshotMillis / 1000)
.setNanos((int) ((snapshotMillis % 1000) * 1000000)).build();
TableModifiers modifiers = TableModifiers.newBuilder().setSnapshotTime(t).build();
sessionBuilder.setTableModifiers(modifiers);
}
// Begin building the session creation request.
CreateReadSessionRequest.Builder builder = CreateReadSessionRequest.newBuilder().setParent(parent)
.setReadSession(sessionBuilder).setMaxStreamCount(1);
ReadSession session = client.createReadSession(builder.build());
// Setup a simple reader and start a read session.
try (SimpleRowReader reader = new SimpleRowReader(session.getArrowSchema())) {
Preconditions.checkState(session.getStreamsCount() > 0);
String streamName = session.getStreams(0).getName();
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder().setReadStream(streamName).build();
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
for (ReadRowsResponse response : stream) {
Preconditions.checkState(response.hasArrowRecordBatch());
reader.processRows(response.getArrowRecordBatch());
}
}
}
}
}
Using your code I can't reproduce the issue.
Creating a test table:
CREATE or REPLACE table abc.num
AS SELECT CAST(123 as numeric) as numeric_datatype,
CAST(123 as bignumeric) as bignumeric_datatype
And then running your code (generating the table URL swapped dataset and table in the pasted code) I get:
ArrowRecordBatch [length=1, nodes=[ArrowFieldNode [length=1, nullCount=0], ArrowFieldNode [length=1, nullCount=0]], #buffers=4, buffersLayout=[ArrowBuffer [offset=0, size=0], ArrowBuffer [offset=0, size=16], ArrowBuffer [offset=16, size=0], ArrowBuffer [offset=16, size=32]], closed=false]
numeric_datatype bignumeric_datatype
123.000000000 123.00000000000000000000000000000000000000
The extra digits after the decimal place are expected because the scale of numeric is 9 and the scale of bignumeric is 38, which means that the full value logically includes those values. The contentToTSVString is just calling toString on a BigDecimal returned from the DecimalVector/Decimal256Vector. If you want to remove the fractional digits you can call getObject the the DecimalVector and call stripTrailingZeros before printing the value.
Relevant dependencies:
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>grpc-google-cloud-bigquerystorage-v1</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-bigquerystorage-v1</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>arrow-memory-netty</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquerystorage</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>arrow-vector</artifactId>
<version>6.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.api/gax -->
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
<version>2.12.2</version>
</dependency>

Migration from Spring Batch 3 to 4

I'm trying to migrate from Spring Batch 3.0.6 to 4.1.1.
I'm stuck handling Spring Batch's execution context data as explained here.
What I would like is update tables batch_step_execution_context and batch_step_execution_context as follows, for each record :
unserialize context data using the deprecated XStreamExecutionContextStringSerializer
serialize the java object obtained using a jackson ObjectMapper
update the table using this new string
It has worked for some records and I could run Spring Batch 4. But for some jobs, the new string is not deserializable as a ExecutionContext.
Here is an example of such a string :
{
"setMoisAffaires":[
{
"premierJourDuMois":{
"date":1257030000000,
"stringFromDatePatternAmericain":"2009-11-01",
"hour":0,
"year":2009,
"dayOfMonth":1,
"dayOfWeek":7,
"dateString":"20091101",
"monthOfYear":11,
"minuteOfHour":0,
"secondOfMinute":0
},
"date":1257030000000,
"stringFromDatePatternAmericain":"2009-11-01",
"hour":0,
"year":2009,
"dayOfMonth":1,
"dayOfWeek":7,
"dateString":"200911",
"monthOfYear":11,
"minuteOfHour":0,
"secondOfMinute":0
},
//a lot of records like the previous...
],
"batch.taskletType":"com.sun.proxy.$Proxy57",
"batch.stepType":"org.springframework.batch.core.step.tasklet.TaskletStep"
}
Here is the trace when I try to do : ExecutionContext a = objectMapper.readValue(objectMapper.writeValueAsString(javaObject),ExecutionContext.class);
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "setMoisAffaires" (class org.springframework.batch.item.ExecutionContext), not marked as ignorable (one known property: "dirty"])
at [Source: (String)"{"setMoisAffaires":[{"prem...[truncated 15163 chars]; line: 1, column: 21] (through reference chain: org.springframework.batch.item.ExecutionContext["setMoisAffaires"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
Here is a unit test to reproduce the problem :
package springBatch4Migration;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import org.junit.Test;
import org.springframework.batch.core.repository.dao.XStreamExecutionContextStringSerializer;
import org.springframework.batch.item.ExecutionContext;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SerializeTest {
#Test
public void test() throws Exception {
String oldJson = "{\"map\":[{\"entry\":[{\"string\":[\"batch.stepType\",\"org.springframework.batch.core.step.tasklet.TaskletStep\"]},{\"string\":[\"batch.taskletType\",\"com.sun.proxy.$Proxy56\"]},{\"string\":\"setMoisAffaires\",\"set\":[{\"tools.date.dates.Mois\":[{\"dateLocale\":{\"iLocalMillis\":1264982400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#resolves-to\":\"org.joda.time.chrono.ISOChronology$Stub\",\"#serialization\":\"custom\",\"org.joda.time.chrono.ISOChronology$Stub\":{\"org.joda.time.UTCDateTimeZone\":{\"#resolves-to\":\"org.joda.time.DateTimeZone$Stub\",\"#serialization\":\"custom\",\"org.joda.time.DateTimeZone$Stub\":{\"string\":\"UTC\"}}}}},\"categorie\":\"MOIS\",\"dateAsString\":201002},{\"dateLocale\":{\"iLocalMillis\":1312156800000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201108},{\"dateLocale\":{\"iLocalMillis\":1288569600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201011},{\"dateLocale\":{\"iLocalMillis\":1285891200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201010},{\"dateLocale\":{\"iLocalMillis\":1243814400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200906},{\"dateLocale\":{\"iLocalMillis\":1257033600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200911},{\"dateLocale\":{\"iLocalMillis\":1277942400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201007},{\"dateLocale\":{\"iLocalMillis\":1249084800000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200908},{\"dateLocale\":{\"iLocalMillis\":1320105600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201111},{\"dateLocale\":{\"iLocalMillis\":1283299200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201009},{\"dateLocale\":{\"iLocalMillis\":1328054400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201202},{\"dateLocale\":{\"iLocalMillis\":1325376000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201201},{\"dateLocale\":{\"iLocalMillis\":1296518400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201102},{\"dateLocale\":{\"iLocalMillis\":1306886400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201106},{\"dateLocale\":{\"iLocalMillis\":1267401600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201003},{\"dateLocale\":{\"iLocalMillis\":1251763200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200909},{\"dateLocale\":{\"iLocalMillis\":1262304000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201001},{\"dateLocale\":{\"iLocalMillis\":1280620800000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201008},{\"dateLocale\":{\"iLocalMillis\":1270080000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201004},{\"dateLocale\":{\"iLocalMillis\":1317427200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201110},{\"dateLocale\":{\"iLocalMillis\":1272672000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201005},{\"dateLocale\":{\"iLocalMillis\":1304208000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201105},{\"dateLocale\":{\"iLocalMillis\":1233446400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200902},{\"dateLocale\":{\"iLocalMillis\":1254355200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200910},{\"dateLocale\":{\"iLocalMillis\":1246406400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200907},{\"dateLocale\":{\"iLocalMillis\":1309478400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201107},{\"dateLocale\":{\"iLocalMillis\":1259625600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200912},{\"dateLocale\":{\"iLocalMillis\":1238544000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200904},{\"dateLocale\":{\"iLocalMillis\":1314835200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201109},{\"dateLocale\":{\"iLocalMillis\":1330560000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201203},{\"dateLocale\":{\"iLocalMillis\":1241136000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200905},{\"dateLocale\":{\"iLocalMillis\":1322697600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201112},{\"dateLocale\":{\"iLocalMillis\":1301616000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201104},{\"dateLocale\":{\"iLocalMillis\":1275350400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201006},{\"dateLocale\":{\"iLocalMillis\":1293840000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201101},{\"dateLocale\":{\"iLocalMillis\":1235865600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200903},{\"dateLocale\":{\"iLocalMillis\":1291161600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201012},{\"dateLocale\":{\"iLocalMillis\":1298937600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201103}]}]}]}]}";
/*
* Unserializing oldJson to a Java object...
*/
XStreamExecutionContextStringSerializer oldSerializer = new XStreamExecutionContextStringSerializer();
oldSerializer.init();
ByteArrayInputStream oldInputStream = new ByteArrayInputStream(oldJson.getBytes());
Object javaObject = oldSerializer.deserialize(oldInputStream);
/*
* Serializing back to a new Json string using Jackson...
*/
ObjectMapper newSerializer = new ObjectMapper();
ExecutionContext a = newSerializer.readValue(newSerializer.writeValueAsString(javaObject),ExecutionContext.class);
assertNotNull(a);
}
}
And here is what I get if I try to run a job using such data :
ERROR - CommandLineJobRunner:367 - Job Terminated in error: Unable to deserialize the execution context
java.lang.IllegalArgumentException: Unable to deserialize the execution context
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao$ExecutionContextRowMapper.mapRow(JdbcExecutionContextDao.java:328)
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao$ExecutionContextRowMapper.mapRow(JdbcExecutionContextDao.java:312)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:61)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:679)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:700)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:712)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:768)
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao.getExecutionContext(JdbcExecutionContextDao.java:129)
at org.springframework.batch.core.explore.support.SimpleJobExplorer.getStepExecutionDependencies(SimpleJobExplorer.java:210)
at org.springframework.batch.core.explore.support.SimpleJobExplorer.getJobExecutions(SimpleJobExplorer.java:87)
at org.springframework.batch.core.JobParametersBuilder.getNextJobParameters(JobParametersBuilder.java:266)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:357)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:564)
at fr.mycompany.myapp.commun.batch.CommandLineJobRunnerTest.runJob(CommandLineJobRunnerTest.java:180)
at fr.mycompany.myapp.commun.batch.CommandLineJobRunnerTest.execute(CommandLineJobRunnerTest.java:496)
at fr.mycompany.myapp.commun.batch.CommandLineJobRunnerTest.main(CommandLineJobRunnerTest.java:529)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (START_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of java.lang.Object)
at [Source: (ByteArrayInputStream); line: 1, column: 21] (through reference chain: java.util.HashMap["setMoisAffaires"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1499)
at com.fasterxml.jackson.databind.DeserializationContext.reportWrongTokenException(DeserializationContext.java:1274)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._locateTypeId(AsArrayTypeDeserializer.java:151)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:96)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:529)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3077)
at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.deserialize(Jackson2ExecutionContextStringSerializer.java:70)
at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.deserialize(Jackson2ExecutionContextStringSerializer.java:50)
at org.springframework.batch.core.repository.dao.JdbcExecutionContextDao$ExecutionContextRowMapper.mapRow(JdbcExecutionContextDao.java:325)
... 18 more
Here is an example of a step using such setMoisAffaires :
<bean id="jobname.jobstep" parent="jobItemParent"
class="fr.mycompany.myapp.collecte.batch.jobname.jobstep" scope="step">
<property name="setMoisAffaires" value="#{stepExecutionContext['setMoisAffaires']}" />
</bean>
What has been serialized with XStream is not necessarily deserializable with Jackson. So I'm not sure how you can solve this issue. See breaking change note here.
EDIT: After adding a minimal complete verifiable example
The XStreamExecutionContextStringSerializer#deserialize returns a Map<String, Object> of the key/value pairs of the old execution context. I would this map directly as input to the Jackson serializer instead of marshalling it to a String and then creating it back from a String. Here is how I updated the test:
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
import org.springframework.batch.core.repository.dao.XStreamExecutionContextStringSerializer;
public class SerializeTest {
#Test
public void test() throws Exception {
String oldJson = "{\"map\":[{\"entry\":[{\"string\":[\"batch.stepType\",\"org.springframework.batch.core.step.tasklet.TaskletStep\"]},{\"string\":[\"batch.taskletType\",\"com.sun.proxy.$Proxy56\"]},{\"string\":\"setMoisAffaires\",\"set\":[{\"tools.date.dates.Mois\":[{\"dateLocale\":{\"iLocalMillis\":1264982400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#resolves-to\":\"org.joda.time.chrono.ISOChronology$Stub\",\"#serialization\":\"custom\",\"org.joda.time.chrono.ISOChronology$Stub\":{\"org.joda.time.UTCDateTimeZone\":{\"#resolves-to\":\"org.joda.time.DateTimeZone$Stub\",\"#serialization\":\"custom\",\"org.joda.time.DateTimeZone$Stub\":{\"string\":\"UTC\"}}}}},\"categorie\":\"MOIS\",\"dateAsString\":201002},{\"dateLocale\":{\"iLocalMillis\":1312156800000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201108},{\"dateLocale\":{\"iLocalMillis\":1288569600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201011},{\"dateLocale\":{\"iLocalMillis\":1285891200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201010},{\"dateLocale\":{\"iLocalMillis\":1243814400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200906},{\"dateLocale\":{\"iLocalMillis\":1257033600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200911},{\"dateLocale\":{\"iLocalMillis\":1277942400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201007},{\"dateLocale\":{\"iLocalMillis\":1249084800000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200908},{\"dateLocale\":{\"iLocalMillis\":1320105600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201111},{\"dateLocale\":{\"iLocalMillis\":1283299200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201009},{\"dateLocale\":{\"iLocalMillis\":1328054400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201202},{\"dateLocale\":{\"iLocalMillis\":1325376000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201201},{\"dateLocale\":{\"iLocalMillis\":1296518400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201102},{\"dateLocale\":{\"iLocalMillis\":1306886400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201106},{\"dateLocale\":{\"iLocalMillis\":1267401600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201003},{\"dateLocale\":{\"iLocalMillis\":1251763200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200909},{\"dateLocale\":{\"iLocalMillis\":1262304000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201001},{\"dateLocale\":{\"iLocalMillis\":1280620800000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201008},{\"dateLocale\":{\"iLocalMillis\":1270080000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201004},{\"dateLocale\":{\"iLocalMillis\":1317427200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201110},{\"dateLocale\":{\"iLocalMillis\":1272672000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201005},{\"dateLocale\":{\"iLocalMillis\":1304208000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201105},{\"dateLocale\":{\"iLocalMillis\":1233446400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200902},{\"dateLocale\":{\"iLocalMillis\":1254355200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200910},{\"dateLocale\":{\"iLocalMillis\":1246406400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200907},{\"dateLocale\":{\"iLocalMillis\":1309478400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201107},{\"dateLocale\":{\"iLocalMillis\":1259625600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200912},{\"dateLocale\":{\"iLocalMillis\":1238544000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200904},{\"dateLocale\":{\"iLocalMillis\":1314835200000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201109},{\"dateLocale\":{\"iLocalMillis\":1330560000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201203},{\"dateLocale\":{\"iLocalMillis\":1241136000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200905},{\"dateLocale\":{\"iLocalMillis\":1322697600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201112},{\"dateLocale\":{\"iLocalMillis\":1301616000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201104},{\"dateLocale\":{\"iLocalMillis\":1275350400000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201006},{\"dateLocale\":{\"iLocalMillis\":1293840000000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201101},{\"dateLocale\":{\"iLocalMillis\":1235865600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":200903},{\"dateLocale\":{\"iLocalMillis\":1291161600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201012},{\"dateLocale\":{\"iLocalMillis\":1298937600000,\"iChronology\":{\"#class\":\"org.joda.time.chrono.ISOChronology\",\"#reference\":\"..\\/..\\/..\\/tools.date.dates.Mois\\/dateLocale\\/iChronology\"}},\"categorie\":\"MOIS\",\"dateAsString\":201103}]}]}]}]}";
/*
* Unserializing oldJson to a Java object...
*/
XStreamExecutionContextStringSerializer oldSerializer = new XStreamExecutionContextStringSerializer();
oldSerializer.init();
ByteArrayInputStream oldInputStream = new ByteArrayInputStream(oldJson.getBytes());
Map<String, Object> javaObject = oldSerializer.deserialize(oldInputStream);
/*
* Serializing back to a new Json string using Jackson...
*/
ObjectMapper newSerializer = new ObjectMapper();
String newJson = newSerializer.writeValueAsString(javaObject);
assertNotNull(newJson);
System.out.println("newJson = " + newJson);
/*
* Or more correctly
*/
Jackson2ExecutionContextStringSerializer stringSerializer = new Jackson2ExecutionContextStringSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
stringSerializer.serialize(javaObject, out);
newJson = new String(out.toByteArray());
assertNotNull(newJson);
System.out.println("newJson = " + newJson);
}
}
Hope this helps.

xpath union operator with VTD XML Parser

Can someone please post an example for using union operator (|) with VTD XML parser ?
Below is not working in VTD XML parser but works in jxpath parser.
/a | /b
Ok, this is a quick example of union expression in action.
import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
/**
* An issue that seems to exist in VTD-XML 2.12 and 2.13 (but not 2.11) that causes lookups for default namespace nodes
* to also pickup non-default namespaced nodes.
*/
public class VtdAutoPilotXpathIssueTest {
private static final String XML = "<a xmlns:x=\"" + "urn:test" + "\"><b id=\"1\"/><x:b id=\"2\"/><b id=\"3\"/></a>";
public static void main(String[] s) throws VTDException{
VTDGen vg = new VTDGen();
vg.setDoc(XML.getBytes());
vg.parse(false);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace("abc", "urn:test");
ap.selectXPath("//b|/a");
int i=0;
while((i=ap.evalXPath())!=-1){
System.out.println(" token "+ vn.toRawString(i));
}
}
}

Error occurred creating story: "Cannot parse object reference from", Rally

I am trying to create user stories in rally by java. Am failing to get response and it throwing " Cannot parse object reference from ".
Brief on my rally : Let assume I have Project name "Characters" and its has subs as A, B, C, D. I have to create my user stories in C. Is my following code correct in this prospect? Can anyone please help?
package samplerally;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.util.Ref;
public class CreateStory {
public static void main(String[] args) throws URISyntaxException,IOException {
String host = "https://rally1.rallydev.com";
String username = "name#comp.com";
String password = "password";
String wsapiVersion = "v2.0";
String applicationName = "C";
RallyRestApi restApi = new RallyRestApi(new URI(host),username,password);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
try {
for (int i = 0; i < 3; i++) {
// Add a story
System.out.println("Creating a story...");
JsonObject newStory = new JsonObject();
newStory.addProperty("Name", "my story");
newStory.addProperty("Project", "Characters");
CreateRequest createRequest = new CreateRequest("hierarchicalrequirement", newStory);
CreateResponse createResponse = restApi.create(createRequest);
System.out.println("Response ::: "+createResponse.wasSuccessful());
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s",createResponse.getObject().get("_ref").getAsString()));
// Read story
String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Story %s...",ref));
GetRequest getRequest = new GetRequest(ref);
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating story: ");
for (int j = 0; j < createErrors.length; j++) {
System.out.println(createErrors[j]);
}
}
}
} finally {
// Release all resources
restApi.close();
}
}
}
And I am getting error as :
Creating a story...
Response ::: false
Error occurred creating story:
Could not read: Cannot parse object reference from "BSS HWSE Team Columbia"
Creating a story...
Response ::: false
Error occurred creating story:
Could not read: Cannot parse object reference from "BSS HWSE Team Columbia"
Creating a story...
Response ::: false
Error occurred creating story:
Could not read: Cannot parse object reference from "BSS HWSE Team Columbia"
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:"C:\Program Files\HP\Unified Functional Testing\bin\java_shared\classes";"C:\Program Files\HP\Unified Functional Testing\bin\java_shared\classes\jasmine.jar"
Please help. New to rally. Thanks in advance
When setting reference fields such as Project, Iteration, Release, WorkProduduct, Parent, etc that point to full objects use the reference,and not the Name.
Find out the unique ObjectID of the Project "C", e.g. 123456.
Replace:
newStory.addProperty("Project", "Characters");
with
newStory.addProperty("Project", "/project/123456");
To find ObjectID of a project you may either query by its name in WS API, or look at the URL of one of Rally pages in the address bar, while in that project, e.g. URL of a Backlog page: https://rally1.rallydev.com/#/123456/backlog
If there is a d or u appended to ObjectID string, e.g. 123456d, do not include it in string. In the URL it indicates if you are currently scoping up or down.

Use property file in a JAR

After a lot of property file articles and comments I am really lost.
All I want is to retrieve values and to overwrite them - and I want to use that with a jar-file.
If I compile in eclipse it works perfectly but the moment I compile I got the famous "property file not found"-exception.
FileInputStream in = new FileInputStream(ClassLoader.getSystemResource("client.properties").getPath());
Properties props = new Properties();
props.load(in);
in.close();
The exception I got is the following:
C:\Users\thomas\Desktop>java -jar erp_auer_client_v0_1.jar
java.io.FileNotFoundException: file:\C:\Users\thomas\Desktop\erp_auer_client_v0_
1.jar!\client.properties (Die Syntax f³r den Dateinamen, Verzeichnisnamen oder d
ie Datentrõgerbezeichnung ist falsch)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at global_functions.helperFunctions.getPathBARTENDEREXE(helperFunctions.
java:361)
at client.programmeinstellungen.ProgrammEinstellungenManagement.<init>(P
rogrammEinstellungenManagement.java:59)
at client.main.MainOverview$11.mousePressed(MainOverview.java:275)
The german part (Die Syntax f³r den Dateinamen, Verzeichnisnamen oder d
ie Datentrõgerbezeichnung ist falsch) means "The syntax for the filename, directory name or disk name is wrong".
Do you have an idea what that could be?
Alright, I tried it myself and realized that it is a little more trickier than I thought initially. However, I think that in your scenario you can just use something like bellow. I tested in windows and linux and worked fine for me:
package mypackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Properties;
public class MyMainClass {
public static void main(String[] args) throws URISyntaxException {
/*
*
* MyMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
* In Linux it returns null
*
* ClassLoader.getSystemClassLoader().getResource(".").getPath() + "MyProperty.properties";
* In Linux it returns {JRE_PATH}/lib/ext/pulse-java.jar
* In Windows it returns {JAR_FILE_PATH}
*/
String propertyFilePath = "MyProperty.properties";
Properties props = new Properties();
try {
FileInputStream in = new FileInputStream(propertyFilePath);
props.load(in);
in.close();
} catch (Exception e) {
File f = new File(propertyFilePath);
System.err.println("Could not read file: " + f.getAbsolutePath());
}
System.out.println("Fetching property value of [now]: " + props.get("now"));
String now = new Date().toString();
System.out.println("Storing property [now]: " + now);
props.setProperty("now", now);
try {
OutputStream out = new FileOutputStream(propertyFilePath);
props.store(out, "Saving value of [now]");
} catch (Exception e) {
File f = new File(propertyFilePath);
System.err.println("Could not write file: " + f.getAbsolutePath());
}
}
}