06/06/2015
[OSGi] Spring error Unable to locate Spring NamespaceHandler for XML schema namespace
You think it would be a good idea if you could join these technologies but reading is so boring, plus since both use some config files you just need to mash them together and you should be good with your Sprosgienstein creation. But then you realize that some small differences for example in classpath handling and dependency management are making your life difficult.
A common error you might get is: "Unable to locate Spring NamespaceHandler for XML schema namespace XXX"
05/09/2014
[Apache Tomcat] Find server version from command line
java -cp catalina.jar org.apache.catalina.util.ServerInfo
from the TOMCAT_INSTALL_DIR/tomcat/lib folder
30/08/2014
[Java] unzip
import java.util.*;
import java.io.*;
import org.apache.commons.compress.archivers.zip.*;
import org.apache.commons.compress.utils.IOUtils;
public void unzip(String ziplocation, String zipname, String unziplocation){
//method variables
private ZipFile zipFile;
private Enumeration<ZipArchiveEntry> entries;
private ZipArchiveEntry entry;
private File entryDestination;
private InputStream in;
private OutputStream out;
zipFile = new ZipFile(ziplocation+File.separator+zipname);
//get zip file content references
entries = zipFile.getEntries();
if(entries != null){
//for each reference, unzip to unziplocation, creating directories if needed
while (entries.hasMoreElements()) {
entry = entries.nextElement();
entryDestination = new File(unziplocation, entry.getName());
entryDestination.getParentFile().mkdirs();
if (entry.isDirectory()){
entryDestination.mkdirs();
}
else {
in = zipFile.getInputStream(entry);
out = new FileOutputStream(entryDestination);
IOUtils.copy(in, out);
//close streams ignoring exceptions
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
}
}
[Java] zip file or directory (with subdirectories)
A simple way to zip a file or folder (with or without subdirectories) maintaining the folders structure is using the Apache Commons Compress library.
import java.util.*;
import java.io.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.archivers.*;
import org.apache.commons.compress.archivers.zip.*;
public void zip(String location, String name, String ziplocation, String zipname){
//method variables
private OutputStream out;
private ArchiveOutputStream zip_out;
private File f;
//out writes the final file, zip_out creates the zip archive
out = new FileOutputStream(new File(ziplocation+File.separator+zipname+".zip"));
zip_out = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
//zip it
File f = new File(location+File.separator+name);
//first time baseDir is empty
dozip(f, "");
//close archive
zip_out.finish();
out.close();
}
//aux method for zipping
private void dozip(File myFile, String baseDir) throws Exception{
//maintain the directory structure while zipping
String entryName = baseDir+myFile.getName();
//DO NOT do a putArchiveEntry for folders as it is not needed
//if it's a directory then list and zip the contents. Uses recursion for nested directories
if(myFile.isDirectory() == true){
File[] filesList = myFile.listFiles();
if(filesList != null){
for (File file : filesList) {
dozip(file, entryName+File.separator);
}
}
}
else{
//add file
zip_out.putArchiveEntry(new ZipArchiveEntry(myFile, entryName));
IOUtils.copy(new FileInputStream(myFile), zip_out);
zip_out.closeArchiveEntry();
}
}
It has to be recursive so that we can handle subdirectories correctly. The resulting zipped file will unzip to the same exact folder structure originally zipped. If you pass the location and ziplocation parameters with the path separator already appended, there's no need to concatenate File.separator in the code.
[Java] move file or directory
Luckily there's the Apache Commons IO library. Using that, you would do something like:
import java.util.*;
import java.io.*;
import org.apache.commons.io.FileUtils;
public void move(String fromDir, String toDir){
//moveToDirectory(File, File, createDestDir), we MUST create two File objects before!
File from = new File(fromDir);
File to = new File(toDir);
/*returns void, throws exceptions:
NullPointerException - if source or destination is null
FileExistsException - if the directory or file exists in the destination directory
IOException - if source or destination is invalid or if an IO error occurs moving the file*/
FileUtils.moveToDirectory(from, to, false);
}
17/12/2013
[Java] Hashing with SHA-256
Update: Hashing this way doesn't make the hash stronger or weaker, but allows us to stream the file while hashing instead of having to wait for the whole operation to complete.
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.security.MessageDigest;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.io.FileUtils;
Also note, we're using Apache commons library to handle our File operations
12/11/2012
Enable Tomcat remote access
Look for this piece:
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log." suffix=".txt"
pattern="common" resolveHosts="false"/>
and set resolveHosts parameter to true. You may also need to add a line to the client machines' hosts file so that the IP can be resolved.
04/07/2012
[Java] JSP Jasper/Tomcat code too large for try statement error
This happens if your JSP contains a lot of code, especially Java code (incapsulated in the <% %> tags) or very very long comments.
In fact, before being presented as an HTML page, the JSP is compiled and in the process, it is enclosed in a try-catch statement which, like any method, has a maximum bytecode size of 64KB (65535 bytes).
The only way to avoid this issue is to split the JSP into multiple pages and then include them using the:
<jsp:include page="file.jsp"/>
directive which compiles the page BEFORE including it instead of:
<% @include file="file.jsp" %>
which embeds the page "as is"
01/02/2012
ECF remote OSGi DS Declarative Services example
For this example we will be using the Eclipse IDE for Java with the ECF Remote Services Target Components plug-in, the Equinox framework and ZooKeeper as a service discovery provider.
31/01/2012
DOSGi DS Distributed OSGi Declarative Services example
In this post we will give a fair example of the workings of the OSGi DS Declarative Services remoted with DOSGi.
We can use either one of the following frameworks:
and the Eclipse IDE (for Java). The code can be launched either inside Eclipse or from a standalone OSGi framework.
30/01/2012
OSGi Blueprint Services example
In this post we will give a fair example of the workings of the OSGi Blueprint Services.
We can use either one of the following frameworks:
and the Eclipse IDE (for Java). The code can be launched either inside Eclipse or from a standalone OSGi framework.
06/07/2011
[PHP] PageRank implementation
To check the results obtained we provide a scri'pt to generate a .gexf file to be opened with Gephi.
Test datasets are available at the Toronto university website.
- PHP
- Gephi
- XML
This method takes longer than the other one, however the result is equally right. Using a sparse matrix instead of the full one may help speed up the process.
Method description:
- Pick the NxN input matrix and make it stochastic, call it A
- Create the P" matrix as alpha*A+(1-alpha)*M with alpha a factor which describes the probability of a random jump and M an NxN matrix filled with values 1/N
- Create starting vector v_0 with length N filled with values 1/N
- Compute v_m = v_m-1*P" where the first time v_m-1 is exactly v_0
- Compute the difference v_m - v_m-1 which should converge to 0
- When the difference doesn't vary over a certain threshold or i iterations have been made, stop. v_m should contain the PageRank for every page
NOTE: Due to Apache and PHP limitations, you may have to modify the php.ini file to grant more memory to the scripts (if you don't want to store the matrix on filesystem like we did) by setting the memory_limit parameter to at least 768MB and raising the maximum script execution time to 5 minutes(300 seconds) with max_execution_time.
2. Iterative method - found on phpir
To use this method you must have the nodes and adj_list files from the chosen dataset.
Method description:
Each page is given a starting PR of 1/N where N is the number of nodes in the graph. Each page then gives to every page it links a PR of current_page_PR/number_of_outbound_links.
It's introduced a dampening factor alpha (0,15) which represents the probability of making a random jump while visiting the graph or when reaching a cul de sac.
PR_new = alpha/n + (1-alpha)*PR_old
Every PR is then normalized between 0 and 1.
The process keeps going until it has reached i iterations or the difference between old and new PR doesn't vary over a certain threshold.
Our implementation is available here.
3. Gephi parser:
To use the parser you you must have the nodes and adj_list files from the chosen dataset. The parser outputs a .gexf file to be opened with Gephi. Here is a sample .gexf file obtained by parsing the first dataset available on the site (the one about abortion).
04/04/2009
Install LAMP on Ubuntu
Note that this guide also applies to other distros, but the commands (I don’t think the package too) will be different. / Questa guida si applica anche ad altre distribuzioni, ma i comandi (non credo anche i pacchetti) saranno differenti.
IN ENGLISH:
LAMP = Linux, Apache, MySQL, PHP. In this guide we’re going to see how to install AMP on a .deb-based distro (Debian, Ubuntu, … )
-First, install Apache. Open a terminal and tape:
sudo apt-get install apache2
Now, open tour browser and type:
localhost
to see if everything went fine.
-Then we can proceed to installing PHP:
sudo apt-get install php5 libapache2-mod-php5
To test if it worked, create a file test.php in /var/www and write in it:
<?php phpinfo(); ?>
Restart apache:
sudo /etc/init.d/apache2 restart
Open your browser and type:
localhost/test.php
Some informations about the php version you just installed should appear.
-Finally we can install MySQL:
sudo apt-get install mysql-server
and phpMyAdmin, a tool to manipulate the database:
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
Now type:
sudo gedit /etc/php5/apache2/php.ini
Search for this line:
;extension=msql.so
and remove the “;”
Thats’it. Restart Apache and you’re done. Note that now Apache and MySQL services will automatically start at boot time.
IN ITALIANO:
LAMP = Linux, Apache, MySQL, PHP. In questa guida vedremo come installare AMP su una distribuzione .deb-based (Debian, Ubuntu, … )
-Iniziamo installando Apache. Aprite un terminale e digitate:
sudo apt-get install apache2
Ora controlliamo che sia tutto a posto, aprite un browser e digitate:
localhost
-Adesso possiamo installare PHP:
sudo apt-get install php5 libapache2-mod-php5
Per vedere se è andato tutto bene, create un file test.php in /var/www scrivendoci dentro:
<?php phpinfo(); ?>
Riavviate apache:
sudo /etc/init.d/apache2 restart
Aprite il browser e digitate:
localhost/test.php
Dovrebbero comparire delle informazioni riguardo la versione di php installata.
-Infine possiamo installare MySQL:
sudo apt-get install mysql-server
e phpMyAdmin, un tool per gestire il database:
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
Ora digitate:
sudo gedit /etc/php5/apache2/php.ini
Cercando questa riga:
;extension=msql.so
e rimuovete il “;”
Fine. Riavviate Apache e siete a posto. Ricordate che ora i servizi Apache e MySQL partiranno automaticamente all’avvio del sistema.