Issue while pushing branch into remote (jgit) - java

Hi i created a branch and added some files on that and checked out that branch after that if I try pushing the branch it is not pushed to remote only inital commit gets pushed each and everytime
Here is the code
private void remotePush(GitDto gitDto, CredentialsProvider credential, File destination)
{
try (Repository repository = repositoryBuilder(destination)) {
try (Git git = Git.open(destination)) {
git.checkout().setName(gitDto.getBranchName()).call(); git.commit().setAll(true).setMessage(gitDto.getCommitMessage()).call();
git.push().setRemote("origin").setCredentialsProvider(credential)
.setRefSpecs(new RefSpec(gitDto.getBranchName() + ":" + gitDto.getBranchName()))
.call();
} catch (RefNotFoundException e)
{
throw new RuntimeException(e);
}
catch (RefAlreadyExistsException e)
{
throw new RuntimeException(e);
}
catch (InvalidRefNameException e)
{
throw new RuntimeException(e);
}
catch (CheckoutConflictException e)
{
throw new RuntimeException(e);
}
catch (GitAPIException e) { throw new RuntimeException(e); }
} catch (IOException e) { throw new RuntimeException(e); }
}
Thanks in advance

Related

narrower catch and broader catch inside of it in Java

I have a following nested try - catch in Java as you see below.
One of my concern is that
First ( and third ) catch is InterruptedException ( and Throwable ) and we have general Exception in this catch block.
I'm wondering this exception structure makes sense or not ( ie.
narrower outside catch, and broader inside catch ).
Catch blocks don't throw an exception, it's just logging.
if this structure
is not good, is there a better way?
public void run() {
try {
} catch (InterruptedException ex) { // narrower
....
try {
...
} catch (Exception e) { // broader
...
} finally {
...
}
} catch (ShutdownSignalException shutdownException) {
try {
} catch (InterruptedException ignored) {
}
} catch (Throwable ex) {
try {
} catch (Exception e) {
}
try {
} catch (Exception e) {
} finally {
}
}}}

Java Card Connecting To a Simulator failed

I am trying to test a Java Card applet to establish a connection to a simulator such as cref:
try {
sckClient = new Socket("localhost", 9025);
InputStream is = sckClient.getInputStream();
OutputStream os = sckClient.getOutputStream();
cad = CadDevice.getCadClientInstance(CadDevice.PROTOCOL_T0, is, os);
} catch (Exception e) {
System.out.println("error");
return;
}
try {
cad.powerUp();
} catch (IOException e) {
e.printStackTrace();
} catch (CadTransportException e) {
System.out.println("error");
try {
sckClient.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
My code get stuck in powerUp without any error or exception.
I am using the sample_device and sample_platform that comes with Java Card Development Kit 3.0.5u1

Java handle errors & exceptions

I have a class that allows to download a file from the internet:
public String download(String URL) {
try {
if(somethingbad) {
// set an error?
return false;
}
}
//...
catch (SocketException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch(InterruptedIOException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
Now, I am calling this function in another class and i want to show a message that will help me figure out why this will not work.
what can i do to display something like this?
HTTPReq r = new HTTPReq("http://www.stack.com/api.json");
if(r.err) {
showMessage(getMessage());
}
and the getMessage() will return the SocketException or IOException or even "empty url" if the URL is empty.
First of all I do not think you need all these:
SocketException, UnsupportedEncodingException, ClientProtocolException since they extend IOException
but if you want you can do this:
public String download(String URL) throws IOException, Exception {
try {
if(somethingbad) {
throws new Exception("My Message);
}
}
catch (IOException e) {
throw e;
}
}
And then in your other file:
try {
// some stuff
}
catch (Exception e) {
// do something with e.getMessage();
}
catch (IOException e) {
// do something with e.getMessage();
}
Instead of just doing e.printStackTrace() inside the catch blocks, throw the exception back like so:
throw e;
Then you can surround the calling code like so:
try {
HTTPReq r = new HTTPReq("http://www.stack.com/api.json");
} catch (Exception e) {
// Show error message
}

How to deal with timeout exception in Java?

Here is my code:
private void synCampaign() {
List<Campaign> campaigns;
try {
campaigns = AdwordsCampaign.getAllCampaign();
for(Campaign c : campaigns)
CampaignDao.save(c);
} catch (ApiException e) {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synCampaign();
e.printStackTrace();
} catch (RemoteException e) {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synCampaign();
e.printStackTrace();
}
}
AdwordsCampaign.getAllCampaign() tries to get some remote resource. This may throw a RemoteException because the internet connection times out. When the exception is caught, I just want the thread to sleep for a while, then try to get the remote resource again.
Is there a problem with my code? Or is there a better way?
Nothing really wrong, but the (potentially infinite) retry loop with recursion (and the stack growing) makes me a little nervous. I'd write instead:
private void synCampaignWithRetries(int ntries, int msecsRetry) {
while(ntries-- >=0 ) {
try {
synCampaign();
return; // no exception? success
}
catch (ApiException e ) {
// log exception?
}
catch (RemoteException e ) {
// log exception?
}
try {
Thread.sleep(msecsRetry);
} catch (InterruptedException e1) {
// log exception?
}
}
// no success , even with ntries - log?
}
private void synCampaign() throws ApiException ,RemoteException {
List<Campaign> campaigns = AdwordsCampaign.getAllCampaign();
for(Campaign c : campaigns)
CampaignDao.save(c);
}
This looks OK except the repetition of code in catch block(be sure of number of retries you want). You may want to create a private method to handle your exception as below:
private void synCampaign() {
List<Campaign> campaigns;
try {
campaigns = AdwordsCampaign.getAllCampaign();
for(Campaign c : campaigns)
CampaignDao.save(c);
} catch (ApiException e) {
e.printStackTrace();
waitAndSync();
} catch (RemoteException e) {
e.printStackTrace();
waitAndSync();
}
}
private void waitAndSync(){
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synCampaign();
}
You indeed cannot catch it as a SocketTimeoutException. What is possible is to catch the RemoteException, retrieve it's cause and check if that's an instanceof SocketTimeoutException.
try{
// Your code that throws SocketTimeoutException
}catch (RemoteException e) {
if(e.getCause().getClass().equals(SocketTimeoutException.class)){
System.out.println("It is SocketTimeoutException");
// Do handling for socket exception
}else{
throw e;
}
}catch (Exception e) {
// Handling other exception. If necessary
}

Insert SuperColumn in Cassandra using thrift

I need some clear examples how I can insert super columns in super column using thrift and java.
#Override
public void insertAllReportsByHost(Map<String, List<IReport>> hostReports) throws DatabaseException {
try {
Cassandra.Client client = getClient();
Map<ByteBuffer, Map<String, List<Mutation>>> mutationsMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
for (Map.Entry<String, List<IReport>> entryReport : hostReports.entrySet()) {
ByteBuffer host = toByteBuffer(entryReport.getKey());
List<IReport> reports = entryReport.getValue();
Map<String, List<Mutation>> keyMutations = new HashMap<String, List<Mutation>>();
List<Mutation> mutations = new ArrayList<Mutation>();
for (IReport report : reports) {
report.getProperties();
Column reportDataColumn = new Column(toByteBuffer("Report Data"));
reportDataColumn.setValue(toByteBuffer(report.toString()));//TODO make another way
reportDataColumn.setTimestamp(System.currentTimeMillis());
Long nano = System.nanoTime();
SuperColumn superColumn = new SuperColumn(toByteBuffer(report.getReportId().toString()), Arrays.asList(reportDataColumn));
ColumnOrSuperColumn col = new ColumnOrSuperColumn();
col.super_column = superColumn;
Mutation m = new Mutation();
m.setColumn_or_supercolumn(col);
mutations.add(m);
}
keyMutations.put(COLUMN_FAMILY, mutations);
mutationsMap.put(host, keyMutations);
}
client.batch_mutate(mutationsMap, ConsistencyLevel.ONE);
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (UnavailableException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (TException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (InvalidRequestException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
} catch (TimedOutException e) {
LOGGER.error(e.getMessage(), e);
throw new DatabaseException(e);
}
}

Categories