So........
This one may sound familiar to you right?
You started working on something and at some point some one tells you: "oh!!!! brilliant upload it Git Hub so I can take a look at it"
And ad that point is when the problem starts.
So how do you update code you are working on (this is an existing project) to a repo you are about to create.
Well this is basically solved in StackOverflow BUT allow me to replicate it here ok?
1. Create the remote repository, and get the URL such asgit://github.com/youruser/somename.git
If your local GIT repo is already set up, skips steps 2 and 3
2. Locally, at the root directory of your source, git init
3. Locally, add and commit what you want in your initial repo (for everything, git add . git commit -m 'initial commit comment')to attach your remote repo with the name 'origin' (like cloning would do)
4. to attach your remote repo with the name 'origin' (like cloning would do) git remote add origin [URL From Step 1]
5. git pull origin master
6. to push up your master branch (change master to something else for a different branch): git push origin master
So this worked pretty well for me to have a look at it.
I case you wander this is the StackOverflow link
Haven't you always find your self googling the same old thing you used to do always when coding? Well that's what ForgettableDev is for: a compound of really stupid stuff that we always forget HOW TO DO and it takes hours to find.
viernes, 7 de septiembre de 2012
martes, 14 de agosto de 2012
Complex types & Cloud connectors with Mule ESB
Hi there, have you ever worked with Mule ESB?
Quite nice, free and open source ESB that you should really check!
Now they have this concept of CloudConnector, which is basically an easy plug and play way to make you ESB talk to other service's API. Here is a list of all of them as of today.
Now this post is not about how to use nor create a CloudConnector. This is just a short post about how to deal with a CloudConnector when its methods returns Complex Types.
A Complex Type it's nothing else than a POJO, BUT the fact that it's a POJO make it some how a little more complex to use it the first time.
Why????
Well because the output of any call to a CloudConnector it's something that you can not use out of the box just because you don't know what does it expose, I mean the getters of the object.
And the problem here is that when you are modifying a Mule configuration file (which is the way to program Mule ESB) there is no autocomplete like in Eclipse to help you.
So, now that I've stablished the problem I shall offer you the solution.
Mule ESB has native support for Groovy scripts, which allows you to access any object.
So let's take for instance a call to the LinkedIn Cloud Connector they offer:
When put together this to tags in you Mule config file you'll be able to check all the methods exposed by this POJO and hopefully find something you can use.
Of course this is nothing more than the use of java reflection but for a newbie in both Java and Mule ESB this post my help you.
So that's it, have fun.
Quite nice, free and open source ESB that you should really check!
Now they have this concept of CloudConnector, which is basically an easy plug and play way to make you ESB talk to other service's API. Here is a list of all of them as of today.
Now this post is not about how to use nor create a CloudConnector. This is just a short post about how to deal with a CloudConnector when its methods returns Complex Types.
A Complex Type it's nothing else than a POJO, BUT the fact that it's a POJO make it some how a little more complex to use it the first time.
Why????
Well because the output of any call to a CloudConnector it's something that you can not use out of the box just because you don't know what does it expose, I mean the getters of the object.
And the problem here is that when you are modifying a Mule configuration file (which is the way to program Mule ESB) there is no autocomplete like in Eclipse to help you.
So, now that I've stablished the problem I shall offer you the solution.
Mule ESB has native support for Groovy scripts, which allows you to access any object.
So let's take for instance a call to the LinkedIn Cloud Connector they offer:
<linkedin:get-profile-for-current-user config-ref="LinkedinConf"/>
When set up properly this should return you data from the profile of the currenent user. And it does but it does so by returning a POJO.
So if you want any useful information from it you should access its getter methods. BUT WAIT I DON'T KNOW THE CODE OF IT, even more important I don't have it.
And that's where Groovy comes in handy, for this will tell you what you can ask for this POJO returned:
<linkedin:get-profile-for-current-user config-ref="LinkedinConf"/>
<logger message="#[groovy:payload.getClass().getDeclaredMethods().toString()]" level="INFO" doc:name="Logger"/>When put together this to tags in you Mule config file you'll be able to check all the methods exposed by this POJO and hopefully find something you can use.
Of course this is nothing more than the use of java reflection but for a newbie in both Java and Mule ESB this post my help you.
So that's it, have fun.
martes, 7 de agosto de 2012
Working with ZIP files in memory
Long time no see right?
Today's topic is ZIP files. I'm going to tell you how to work with ZIP files in memory.
The use case comes handy when your code has no access to the actual file persisted in the file system. The most common scenario, I think, it's when a file it's being uploaded or transferred through any end point.
As it turns out Java has quite a lot of library build in in its java.util.zip packages to make this magic happens (kind of logical it's in the zip package eh? I just DIDN'T see it before :P ).
These are the main classes we are going to be using:
Today's topic is ZIP files. I'm going to tell you how to work with ZIP files in memory.
The use case comes handy when your code has no access to the actual file persisted in the file system. The most common scenario, I think, it's when a file it's being uploaded or transferred through any end point.
As it turns out Java has quite a lot of library build in in its java.util.zip packages to make this magic happens (kind of logical it's in the zip package eh? I just DIDN'T see it before :P ).
These are the main classes we are going to be using:
- java.util.zip.ZipEntry
- java.util.zip.ZipInputStream
- java.util.zip.ZipOutputStream
As you may already notice, if we can access the actual file we should have access to something right?
Well that something it's an InputStream, not any input stream of course but a ZipInputStream, so lets see how to create one out of any common InputStream:
InputStream zipFileInputStream;
ZipInputStream inputZipFile = new ZipInputStream(zipFileInputStream);
Pretty easy, right?
Of course, bear in mind that the use case here assumes that you have access to an input stream, could be the actual file, an http etc. The point is that ones you have the input stream you won't have to use anything else to mange the ZIP file.
Now we are going to see two use cases that may come handy, first to read a particular file/s inside a ZIP file and second how to add content to the ZIP file.
Let us begin with the first use case how to get a particular file from inside the ZIP file:
ZipEntry entryFile;
try {
entryFile = inputZipFile.getNextEntry();
while (entryFile != null) {
String[] nameAndExtention = StringUtils.split(entryFile.getName(), ".");
if (nameAndExtention.length >= 2) {
String extension = nameAndExtention[nameAndExtention.length - 1].toLowerCase();
if (extension.equals(FILE_EXTENTION)) {
int n;
byte[] buf = new byte[1024];
ByteArrayOutputStream tmpOutStream = new ByteArrayOutputStream(1024);
while ((n = inputZipFile.read(buf, 0, 1024)) > -1) {
tmpOutStream.write(buf, 0, n);
}
fileList.add(new String(tmpOutStream.toString()));
}
}
entryFile = inputZipFile.getNextEntry();
}
} catch (IOException e) {
e.printStackTrace();
}
So as you can see the first thing we use here is the ZipEntry class. This is what the ZipFileInputStream returns when we iterate through it. It represents a file inside the the ZIP file and its related metadata.
So from this point onwards it's quite simple you'll j just need to play with the ZipEntry API to get what you need.
In my case I was looking for files with a certain extension(word of advise though the getName method returns the canonical name of the file).
So once you selected the files you needed you may want to read them right?
As you may have guessed by now this is achieved by this code:
int n;
byte[] buf = new byte[1024];
ByteArrayOutputStream tmpOutStream = new ByteArrayOutputStream(1024);
inputZipFile.getNextEntry()
while ((n = inputZipFile.read(buf, 0, 1024)) > -1) {
tmpOutStream.write(buf, 0, n);
}
fileList.add(new String(tmpOutStream.toString()));
And the first question should be but how does the ZipFileInpuStream which file to read and the answer it's easy.
When you do:
This works as a pointer for the ZipFileInpuStream, and internally position the pointer to the beginning of the entry it just returned to you. So when you do getNextEntry the pointer moves forward to the next entry.
Thus when you do:
inputZipFile.read
You are going to be reading until the end of the entry you are on, this is the end of the file you selected.
Finally I just do a tmpOutStream.toString() but because the files I work with are text based files.
In this way you can read the content of any file inside a ZIP file.
Now for the second use case, add content to the ZIP file.
The idea it's pretty much the same BUT you'll have to do something like this:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputFile = new ZipOutputStream(outputStream);
ZipEntry newZipentry = new ZipEntry("some_name");
zipOutputFile.putNextEntry(newZipentry);
zipOutputFile.write(newFileContent.getBytes());
As you can see you first need to create an output file first.
So you'll say "BUT YOU SAY ADD CONTENT TO A ZIP FILE" well we ARE doing that it's just a matter of creating your zipOutpuFile from an original content.
Any way I hope you've enjoy it.
Also take a look at the following web sites I used to code this:
miércoles, 18 de julio de 2012
Run Single Test in Maven
If you guys read the reason I started this blog, you'll recall that it was just to summarize stupid stuff one always forget when coding.
And so here is one of them, how to run just a single test from the command line with maven.
Well let me just C&P the data from the Apache Maven site
And so here is one of them, how to run just a single test from the command line with maven.
Well let me just C&P the data from the Apache Maven site
To run this through Maven, set the test property to a specific test case.
mvn -Dtest=TestCircle test
The value for the test parameter is the name of the test class (without the extension; we'll strip off the extension if you accidentally provide one).
You may also use patterns to run a number of tests:
mvn -Dtest=TestCi*le test
And you may use multiple names/patterns, separated by commas:
mvn -Dtest=TestSquare,TestCi*le test
I know I know this has not actual merit but hey think of this blog as place were you can find this kind of things because, except from one or two original things this is what you'll find.
martes, 17 de julio de 2012
Stream of bytes
Today's post is a short one.
So I was playing with streams, and by playing I mean I had to deal with them at work :P
Any way, I do know that unless you are absolutely sure about the content of the stream you should treat them as bytes always, but I made the mistake to handle them as string.
Any way that lead me to find Apache Commons IO, in particular IOUtils.
As it turns out I needed to read all the content from an HTTPInputStream and then returned as byte array.
For those of you who don't see the problem here, the thing is that it's a pain to read something of an input stream at place it in an array. Just to much code for something so simple. Even more if it's a byte array.
Any way a good friend of mine introduce me to this portion of the Apache common library.
(Do take a look at his blog it's just awesome)
And as a result here are the two lines of code that you'll need to do just that i.e. read from an input stream and place the content in a byte array:
So I was playing with streams, and by playing I mean I had to deal with them at work :P
Any way, I do know that unless you are absolutely sure about the content of the stream you should treat them as bytes always, but I made the mistake to handle them as string.
Any way that lead me to find Apache Commons IO, in particular IOUtils.
As it turns out I needed to read all the content from an HTTPInputStream and then returned as byte array.
For those of you who don't see the problem here, the thing is that it's a pain to read something of an input stream at place it in an array. Just to much code for something so simple. Even more if it's a byte array.
Any way a good friend of mine introduce me to this portion of the Apache common library.
(Do take a look at his blog it's just awesome)
And as a result here are the two lines of code that you'll need to do just that i.e. read from an input stream and place the content in a byte array:
import org.apache.commons.io.IOUtils;
return IOUtils.toByteArray((InputStream) yourObjecStream.getStream());
So that's it, please note the casting of the yourObjectStream is just a copy paste hehe.
Hope you enjoy it.
jueves, 12 de julio de 2012
XML & JDom
Hi there!!!!
So todays subject is XML parsing.
Right, you'll say this is a common thing, who didn't work with XMLs.
And you are right, but, as it turns out my line of work didn't usually take me to work with xml files.
Thing is I needed to take an XML string and just add it a new node.
This example it's quite simple except in my case I needed to use name spaces, for the xml file used them.
So with out further due here it is:
Namespace atomNS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
Element newValue = new Element("value").setNamespace(cmisNS).addContent(uuid);
So todays subject is XML parsing.
Right, you'll say this is a common thing, who didn't work with XMLs.
And you are right, but, as it turns out my line of work didn't usually take me to work with xml files.
Thing is I needed to take an XML string and just add it a new node.
This example it's quite simple except in my case I needed to use name spaces, for the xml file used them.
So with out further due here it is:
SAXBuilder builder = new SAXBuilder();
Document document;
document = builder.build(new ByteArrayInputStream(xmlMessage.getBytes()));
Namespace atomNS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
Namespace cmisraNS = Namespace.getNamespace("cmisra", "http://docs.oasis-open.org/ns/cmis/restatom/200908/");
Namespace cmisNS = Namespace.getNamespace("cmis", "http://docs.oasis-open.org/ns/cmis/core/200908/");
Element newElement = new Element("propertyString").setAttribute("propertyDefinitionId", "aNameSpace:uuid");
newElement.setNamespace(cmisNS);
Element newValue = new Element("value").setNamespace(cmisNS).addContent(uuid);
newElement.addContent(newValue);
Element rootElement = document.getRootElement();
rootElement.getChild("object", cmisraNS).getChild("properties", cmisNS).addContent(newElement);
As you can see there is no much new here, even more if you compare it to this site that I used as a starting point.
The only difference is that as my XML has namespaces you must use them in order to JDom parse the document correctly. This is, to get the actual node you are looking for.
In the same way when you create your new elements/nodes you must specify the name space that should be used.
martes, 3 de julio de 2012
EasyMock & Matchers
Hi there!
If you are down this rode chances are you were looking for something related to EasyMock right??
In short (for those who don't know) EasyMock is a mocking framework, makes sense ah?
I suggest you check mocking in Wikipedia, you are lost at this point.
For the rest of you here is the actual point of this post.
Haven you ever find your self mocking a method call where you know ALMOST ALL the values of the parameters that the method would be receiving. Please note that I set almost.
Well thing is, for those values we don't know Easy Mock provide us with matchers. It is a way to say "hey you're going to be receiving, let's say, a String".
Don't really know which one, don't know the content of it and I JUST DON'T CARE.
Here is how you can do that:
Now this makes sense right!!!!
BUT IT DOESN'T
You see, although I'm not laying to you, and Easy Mock does allow you to do such things the above it's not the correct way.
EasyMock allow you to use matchers in your method call BUT
Either all the parameters are passed through Matchers or not. You can't have mixed things as before.
So here are two valid calls:
What if I know some of the value an I don't know some of them. More important I don't know some of the values but at least one must be a particular value.
Well Easy Mock does also provide you with a way to do that, by the use of the following matcher:
EasyMock.eq("");
So the invalid call above should turn into this:
EasyMock.expects(obj.myMethod(EasyMock.eq(""), EasyMock.anyObject(String.class));
In this way you can keep on mocking.
For those of you that may be googling this article the exception thrown when doing this is:
If you are down this rode chances are you were looking for something related to EasyMock right??
In short (for those who don't know) EasyMock is a mocking framework, makes sense ah?
I suggest you check mocking in Wikipedia, you are lost at this point.
For the rest of you here is the actual point of this post.
Haven you ever find your self mocking a method call where you know ALMOST ALL the values of the parameters that the method would be receiving. Please note that I set almost.
Well thing is, for those values we don't know Easy Mock provide us with matchers. It is a way to say "hey you're going to be receiving, let's say, a String".
Don't really know which one, don't know the content of it and I JUST DON'T CARE.
Here is how you can do that:
The method firm: public void myMetod(String firstStr, String secondStr)
Easy mock call: EasyMock.expects(obj.myMethod("", EasyMock.anyObject(String.class));
Now this makes sense right!!!!
BUT IT DOESN'T
You see, although I'm not laying to you, and Easy Mock does allow you to do such things the above it's not the correct way.
EasyMock allow you to use matchers in your method call BUT
Either all the parameters are passed through Matchers or not. You can't have mixed things as before.
So here are two valid calls:
EasyMock.expects(obj.myMethod(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class)); EasyMock.expects(obj.myMethod("", "");Now all this brings us to the main problem:
What if I know some of the value an I don't know some of them. More important I don't know some of the values but at least one must be a particular value.
Well Easy Mock does also provide you with a way to do that, by the use of the following matcher:
EasyMock.eq("");
So the invalid call above should turn into this:
EasyMock.expects(obj.myMethod(EasyMock.eq(""), EasyMock.anyObject(String.class));
In this way you can keep on mocking.
For those of you that may be googling this article the exception thrown when doing this is:
java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
Any way, I hope you have enjoy this!
Please also check this article that help me to find out this
Suscribirse a:
Entradas (Atom)