First of all I am new to Java programming and object oriented style programming. I started learning it on January this year.
Basically i have a class name vehicleInformation with some local variable.
Then I i prompt the information in the client side and then store it in a object vehicleInformation. Then pass the object to the server side. My instruction is to insert the data in the object to the database. My question is how do i access to the data in the object when it is pass from the client side.
the code below show how i create the object and send it to the server.
vehicleInformation v = new vehicleInformation(plateNumber,vehicleType, engineNumber, chassisNumber, make, model);
toServer.writeObject(v);
the code below is how i read the object but I have no idea how to access to the variable in the obect as we normally use objectName.variable to access it.
Object object = inputFromClient.readObject();
To be safe you should check the type of the object before the typecast.
if (object instanceof VehicleInformation) {
vehicleInformation = (VehicleInformation) object;
}
else {
// Do something with the unexpected object type. e.g. throw an exception.
...
}
You can typecast
VehicleInformation vehicleObject = (VehicleInformation)inputFromClient.readObject();
Cast your object to vehicleInformation.
vehicleInformation objectname = (vehicleInformation)inputFromClient.readObject();
and get variable objectname.variable
And follow class naming convention for better visibility.
Related
Can serializing an object to JSON and immediately deserializing it back to the original object type be a valid way to deep copy an object.
I am asking mostly for languages like C# and Java, but would this be different across different languages?
Are there any issues that might occur by doing this?
Seems logical, but might be a bit inefficient. I take it the object in question is just a simple poco.
You could use reflection to do this too.
This code will copy the properties of an object and return an instance of the new copy
public TTranslateTo TranslateTo<TTranslateTo>()
{
var target = Activator.CreateInstance<TTranslateTo>();
foreach (var p1 in GetObjectTypeProperties)
{
var p2 =
target.GetType()
.GetProperties()
.FirstOrDefault(p => string.Equals(p.Name, p1.Name, StringComparison.CurrentCultureIgnoreCase) && p.PropertyType == p1.PropertyType);
p2?.SetValue(target, p1.GetValue(this));
}
return target;
}
private IEnumerable<PropertyInfo> GetObjectTypeProperties => GetType()
.GetProperties();
You could include this code in a base class giving you access to copy functions on all objects.
I made a Java program which allows to create instances Programatically.
I need to parse the return object to print the public IP address of the Instance.
However when I output the result of the describeInstances() function of Amazon's EC2 client, the output is a maze of lists and hash maps and I don't know how to parse it.
Can anybody tell me a more simpler approach to accomplish this?
I tried to convert the Ec2.describeInstances().getReservations() result to a string and then manipulate the string to output the Public IP address.
Is there any simpler way to accomplish this?
Code:
DescribeAddressesRequest add =new DescribeAddressesRequest();
String Desc= client.describeInstances().getReservations().get(1).toString();
The SDK doesn't return HashMaps it returns actual Java classes. I'm not sure how you are getting HashMaps out of it. Converting the returned object to a String and manipulating that is definitely not the recommended approach.
If you look at the API docs you will see that describeInstances() returns a DescribeInstancesResult which contains a list of Reservation objects, which each contain a list of Instance objects. The Instance object has a getPublicIpAddress() method. So you could do something like the following:
DescribeInstancesRequest request = new DescribeInstancesRequest();
String ipAddress = client.describeInstances(request) // You pass the request here
.getReservations().get(0) // Get the first reservation
.getInstances().get(0) // Get the first instance in the reservation
.getPublicIpAddress(); // Get the public IP address of the instance
I assume you're adding some criteria, like the reservation ID, to the DescribeInstancesRequest object so that you can expect only one instance to be in the response.
Note that the public IP address might not be assigned immediately. You may have to do this in a loop, checking if the IP address has been assigned yet.
You need an Instance class
getPublicIpAddress()
The public IPv4 address assigned to the instance, if applicable
I don't know the API, but from a Reservation, you get to an Instance.
getInstances()
One or more instances
for (Reservation r : client.describeInstances().getReservations()) {
for (Instance i : r.getInstances()) {
String ipv4 = i.getPublicIpAddress();
}
}
When exporting an object I find that both this
LocateRegistry.createRegistry(1099);
ObjectToExport obj = new ObjectToExport();
UnicastRemoteObject.exportObject(obj, 1099);
Naming.rebind("ObjectName", obj);
and this
LocateRegistry.createRegistry(1099);
ObjectToExport obj = new ObjectToExport();
Naming.rebind("ObjectName", UnicastRemoteObject.exportObject(obj, 1099));
work. In the first I don't use the return value of exportObject and in the second I do. Is there a difference between these 2 ways of exporting an object?
The API only says that the return value is the remote object stub.
There is no difference at the export step, but you're also binding the object, and there's a difference at this step. In the first step you're passing the actual object; in the second, the stub. However the semantics of RMI are that exported remote objects are passed to remote methods as their own stubs, so the actual effect at the Registry is the same.
I'm trying to read the public certificate names from a smartcard to display to the user before they sign a file using a gemalto smartcard.
I've followed the getInfo example from iaikPkcs11Wrapper demos as below :
Module pkcs11Module = Module.getInstance(settings.getCryptoDll());
Slot[] slotList;
try{
slotList = pkcs11Module.getSlotList(true);
}catch(TokenException tex){//module is not initialised
tex.printStackTrace();
pkcs11Module.initialize(new DefaultInitializeArgs());
slotList = pkcs11Module.getSlotList(true);
}
for (Slot slot : slotList) {
Token token = slot.getToken();
iaik.pkcs.pkcs11.Session session = token.openSession(true, SessionReadWriteBehavior.RO_SESSION, null, null);
session.findObjectsInit(null);
Object[] objects = new Object[0];
try {
objects = session.findObjects(1);
This fails always at the line objects = findObjects(1); with a CKR_TEMPLATE_INCONSISTENT exception.
As I understand from the documentation session.findObjectsInit(null) should just return all accessible objects on the card and you can then compare them for type.
I have various smartcards and they all fail like this, I've also tried calling session.findObjectsInit(tempObj) with a GenericTemplate object and a X509PublicKeyCertificate which both return the same exception, and with an X509AttributeCertificate which returns no objects but does not throw the exception.
I'd appreciate any pointers anyone can give. Or do I need to create a matching template object using GenericTemplate? I'm unsure why I'm getting the exception as I thought passing the object into the getObjectInit method filtered for thet object so anything returned should match.
EDIT
I've subsequently tried with other templates and ones for objects not on the card just return an empty array- no exception and ones I think are on the cards just throw the ckr_template_inconsistent exception, any help would be gratefully received.
EDIT2
I've now tried with some new 'V3' cards, which do infact work, all my test cards work using another technique (we currently use capicom via com4J for signing), so maybe there is an issue with the iaik wrapper, or gclib.dll (or me).
i have a list of objects:
List<TaskDataValue> configuredTaskData = sourceofdata;
I am iterating the list, to change a property from an object WITHIN the object:
for (int i=0; i<configuredTaskData.size(); i++) {
FieldConfiguration fc = configuredTaskData.get(i).getSettings();
String fieldName = configuredTaskData.get(i).getName();
if (newLabels.containsKey(fieldName)) {
fc.setLabel(newLabels.get(fieldName));
configuredTaskData.get(i).setSettings(fc);
}
}
although what is happening is that every TaskDataValue.getSettings.label is set to the last one,
sounds like configuredTaskData.get(i).setSettings(fc); is setting not only to i but to evry one
What can be happening here ?
using java 1.6
Check where you create the TaskDataValues in sourceofdata. Is it possible that they all have been configured with the same FieldConfiguration instance?
The most likely reason for something like this to happen is that you only actually have one TaskDataValue object - or inside TaskDataValue there is an object that there is only one of.
Because that object is shared any change made to it from one reference is also seen from all the other references.
For example:
List<TaskDataValue> tdvs = ...;
TaskDataValue v = new TaskDataValue();
tdvs.add(v);
tdvs.add(v);
tdvs.add(v);
That code creates one single TaskDataValue object and adds three references to that object to the list. If you change a setting inside tdvs.get(1) you will also see 2 and 3 change too.