Cant get Castor's MappingTool to work - java

I’m trying to auto generate a mapping file using this program using Castor 1.3.2.
But here is the exception I get -
java.lang.IllegalArgumentException: No enum const class org.exolab.castor.mapping.xml.types.BindXmlNodeType.element
This is a fairly basic test, what am I doing wrong?
public class CastorMapping {
public CastorMapping()
{
try
{
MappingTool tool = new MappingTool();
tool.setInternalContext(new org.castor.xml.BackwardCompatibilityContext());
tool.addClass(TestRequest.class);
OutputStream file = new FileOutputStream("gen_mapping.xml" );
Writer writer = new OutputStreamWriter(file);
tool.write(writer);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
new CastorMapping();
}
}
Thanks!

I tried this myself and I believe you are doing everything correctly.
I browsed the castor source code and as far as I can tell, they broke the MappingTool somewhere between 1.3 and 1.3.2 when they redesigned BindXmlNodeType to be an enum class instead of a regular class. There is some code where they are looking for an BindXmlNodeType.element, but now that BindXmlNodeType is an enum they need to look up ELEMENT (caps). But I digress...
If you can afford to revert to castor 1.3, everything should work.
BTW - I tried to upgrade to 1.3.3-rc1 but Intellij could not resolve the maven dependencies. For example castor-xml in 1.3.3-rc1 now depends on Spring! It's possible that this bug is fixed in a later version, but I am not hopeful.

Related

Java Serialization for Dummies

I'm still working on the project I already needed a bit of help with:
JavaFX - TableView doesn't update items
Now I want to understand how this whole Serialization process in Java works, because unfortunately, I don't really get it now.
Before I go on, first of all, I'm a student, I'm not a professional. Second, I'm neither familiar with using DBs, nor XML or JSON, so I'd just like to find solution to my approach, no matter how inelegant it might be in the end, it just needs to work. So please don't feel offended if I just reject any advice in using other techniques.
So here's what I want:
Saving three different class objects to separate files BUT maintaining backward compatibility to each of it. The objects are Settings, Statistics and a "database" object, containing all words in a list added to it. In the future I may add more statistics or settings, means adding new variables, mostly type of IntegerProperty or DoubleProperty.
Now the question is: is it possible to load old version saved files and then during the process just initiate new variables not found in the old version with just null but keep the rest as it has been saved?
All I know is that the first thing to do so is not to alter the serialVersionUID.
Another thing would be saving the whole Model object (which contains the three objects mentioned before), so I just have to implement stuff for one class instead of three. But how would that work then concerning backward compatibility? I mean the class itself would not change but it's attributes in their own class structure.
Finally, what approach should I go for? And most of all, how do I do this and maintaning backward compatibilty at the same time? I do best with some concrete examples rather than plain theory.
Here are two example methods, if it's of any help. I already have methods for each class to write and read an object.
public static void saveModel(Model model, String destination) throws IOException
{
try
{
fileOutput = new FileOutputStream(destination);
objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(model);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (objectOutput != null)
try
{
objectOutput.close();
}
catch (IOException e) {}
if (fileOutput != null)
try
{
fileOutput.close();
}
catch (IOException e) {}
}
}
public static Settings readSettings(String destination) throws IOException, FileNotFoundException
{
Settings s = null;
try
{
fileInput = new FileInputStream(destination);
objectInput = new ObjectInputStream(fileInput);
Object obj = objectInput.readObject();
if (obj instanceof Settings)
{
s = (Settings)obj;
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
finally
{
if (objectInput != null) try { objectInput.close(); } catch (IOException e) {}
if (fileInput != null) try { fileInput.close(); } catch (IOException e) {}
}
return s;
}
Tell me if you need more of my current code.
Thank you in advance!
... you must be this tall
Best advice for Serialisation is to avoid it for application persistence, especially if backwards compatibility is desired property in your application.
Answers
Is it possible to load old version saved files and then during the process just initiate new variables not found in the old version with just null but keep the rest as it has been saved?
Yes. Deserialising objects saved using previous versions of the class into a new version of this class will work only if:
fully qualified name of the class has not changed (same name and package)
previous and current class have exactly the same serialVersionUID; if one of the versions is missing it, it will be calculated as a 'hash' of all fields and methods and upon a mismatch deserialisation will fail.
inheritance hierarchy has not changed for that class (the same ancestors)
no fields have been removed in the new version of the class
no fields have become static
no fields have become transient
I just have to implement stuff for one class instead of three. But how would that work then concerning backward compatibility?
Yes. Providing that all classes of all fields of Model and Model class itself adhere to the rules above.
Finally, what approach should I go for? And most of all, how do I do this and maintaning backward compatibilty at the same time?
Yes, as long as you can guarantee all of the above rules forever, you will be backwards compatible.
I am sure you can appreciate that forever, or even for next year can be very hard to guarantee, especially in software.
This is why people do application persistence using more robust data exchange formats, than binary representation of serialised Java objects.
Raw data for the table, could be saved using anything from CSV file to JSON docs stored as files or as documents in NoSQL database.
For settings / config have a look at Java's Properties class which could store and load properties to and from *.properties or *.xml files or separately have a look at YAML.
Finally for backwards compatibility, have a look at FlatBuffers
The field of application persistence is very rich and ripe, so happy exploring.

What does EOF FOUND '}' mean in Check style?

I'm using intellij communicated version and also add checkstyle plugin
how ever, i made simple java file just say hello
public class hello {
public static void main(string[] final args)
{
system.out.println("hello, world");
}
}
it's simply run. however checkstyle tell me there is problem at the last line.
he told me
Got an exception-expecting EOF, Found '}' error
I don't know what is the problem. block is correctly close.
is there something i need to add or fix that ?
This problem became a real issue in the checkstyle project. Basically it was related with the use of lambdas, yet the problem exposed herebefore is clarely not related to Java 8 lambdas. You can check the issue here. You can solve it by specifying a valid version. In gradle it would be:
apply plugin: 'checkstyle'
checkstyle {
toolVersion = "6.1.1"
}
Try formatting it like
public class Hello
{
public static void main(String[] args)
{
System.out.println("hello, world");
}
}
and the "s" in system.out.println and string[], should be capital
Checkstyle is a tool which is used to find flaws in formatting and coding conventions followed if any exists in the code. The rules are set using by configuring the checkstyle. And if any part of your code does not abide by them, it will throw an exception. In most of the cases exceptions will be self explanatory. You can use google depending on the exception you get.

Sqoop, export HDFS to MySQL in Java

I'm trying to export from HDFS into MySql and have only been able to find the following technique:
public static boolean exportHDFSToSQL() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
try {
SqoopOptions options = new SqoopOptions();
options.setConnectString("jdbc:mysql://localhost:3306/dbName");
options.setUsername("user_name");
options.setPassword("pwd");
options.setExportDir("path of file to be exported from hdfs");
options.setTableName("table_name");
options.setInputFieldsTerminatedBy(',');
options.setNumMappers(1);
new ExportTool().run(options);
} catch (Exception e) {
return false;
}
return true;
}
The problem I have is with the ExportTool().run() method. I am using Sqoop 1.4.2 and this method has apparently been deprecated. Wanting to know the new way of achieving this? Or point me to a documented source that will assist.
Thanks
Sqoop currently do not exposes any Java API and thus such usage is not supported. It might work, however future versions might break this behavior.
I would expect that you see the deprecation because you are using ExportTool class from package com.cloudera.sqoop.tool, whereas the functionality was moved to package org.apache.sqoop.tool and the original instance was left there for backward compatibility. You can learn more about the namespace migration on appropriate Sqoop wiki page.

What does this serialization error mean, and how can I prevent it?

I am coding in GWT 2.3 using Eclipse. While I have had coding experience, it has been limited to client-side. My current project involves creating a mapping program, which takes a list of points from an Excel sheet and places them on a predefined image. Now, I have my servlet and my client code connected, and I already have some idea how to read the Excel file.
My current problem: I get the following error when I load my application on Firefox using Development Mode:
Something other than an int was returned from JSNI method '#com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader::readInt()': JS value of type undefined, expected int
Development Mode's console doesn't give me any errors when I run, those it does tell me there is a [WARN] with two things I'm not using (images which I misnamed, but do not load ever).
Currently, my code is as follows:
In my Floor.java client side code:
MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class);
AsyncCallback<String> callback = new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
printerModel.setText("FAILED");
String details = caught.getMessage();
printerModel.setText(details);
}
#Override
public void onSuccess(String result) {
//I purposefully have this as an empty method so I could figure out the error
}
};
service.readFile("PrinterList.xls", callback);
In my MyService.java:
>public String readFile(String s);
In `MyServiceImpl.java`:
>public String readFile(String s) {
// TODO Auto-generated method stub
try {
} catch (Exception e) {
}
return "foo";
}
My AsyncCallback type is String, which seems to be causing the error. The method my client code calls returns a single String at this point, "fubar" (for simplicity). I thought that Strings were automatically serializable, but I am not sure. So, how do I get this error to go away? And how do I make the server code serialized?
What the exception says is basically this:
Client was trying to read an object from the data stream. Based on the signature of called method (or some other hint) the stream reader was expecting an int but found undefined instead.
As for the serializability of String, your assumption is correct. They are serializable without any effort on your part.
Without looking at the code and/or exception trace, it's difficult to say anything more.
EDIT:
Your code seems fine to me. Is there a chance that you are mixing GWT versions? That is you compiled your GWT application with 2.3, but the server classpath contains an older GWT jar (or vice versa). Take a look at:
Project GWT version settings. Project-> Properties -> Google -> Web Toolkit. Which version of GWT is selected there?
Compare the GWT settings with Project -> Properties -> Java Build Path -> Libraries. How many GWT related jars do you see there? Which version? Are there more than one gwt-servlet-x.y.jar?

What happened to URIUtil.encodePath from commons-httpclient-3.1?

I want to do what's described in question 724043, namely encode the path components of a URI. The class recommended to do that is URIUtil from Commons HttpClient 3.1. Unfortunately, that class seems to have disappeared from the most recent version of HttpClient. A similarly named class from HttpClient 4.1, URIUtils, doesn't provide the same functionality. Has this class/method been moved to some other library that I'm not aware of or is it just gone? Am I best off just copying the class from the 3.1 release into my code or is there a simpler way?
The maintainers of the module have decreed that you should use the standard JDK URI class instead:
The reason URI and URIUtils got replaced with the standard Java URI was
very simple: there was no one willing to maintain those classes.
There is a number of utility methods that help work around various
issues with the java.net.URI implementation but otherwise the standard
JRE classes should be sufficient, should not they?
So, the easiest is to look at the source of encodePath from the 3.1 release and duplicate what it does in your own code (or just copy the method/class into your codebase).
Or you could go with the accepted answer on the question you referred to (but it seems you have to break the URL into parts first):
new URI(
"http",
"search.barnesandnoble.com",
"/booksearch/first book.pdf",
null).toString();
This can be achieved using org.apache.http.client.utils.URIBuilder utility in httpclient-4.X () as follows.
public static String encodePath(final String path) {
if(path.length() == 0)
return "";
else
return new URIBuilder().setPath(path).toString();
}
You can use Standard JDK functions, e.g.
public static String encodeURLPathComponent(String path) {
try {
return new URI(null, null, path, null).toASCIIString();
} catch (URISyntaxException e) {
// do some error handling
}
return "";
}

Categories