digitalocean spaces java example

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class Test {

    private static final String SUFFIX = "/";

    public static void main(String[] args) {
        AWSCredentials credentials = new BasicAWSCredentials(
                "xxx",
                "yyy");

        AmazonS3 s3client = new AmazonS3Client(credentials);
        s3client.setEndpoint("https://ams3.digitaloceanspaces.com");
        String bucketName = "ubi-example-bucket";
        s3client.createBucket(bucketName);

        for (Bucket bucket : s3client.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }

        String folderName = "testfolder";
        createFolder(bucketName, folderName, s3client);

        String fileName = folderName + SUFFIX + "test.docx";
        s3client.putObject(new PutObjectRequest(bucketName, fileName,
                new File("/home/icdu/projects/link/amazonTest/test.docx"))
                .withCannedAcl(CannedAccessControlList.PublicRead));

//        deleteFolder(bucketName, folderName, s3client);
//        s3client.deleteBucket(bucketName);
    }

    public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(0);

        InputStream emptyContent = new ByteArrayInputStream(new byte[0]);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
                folderName + SUFFIX, emptyContent, metadata);

        client.putObject(putObjectRequest);
    }


//    public static void deleteFolder(String bucketName, String folderName, AmazonS3 client) {
//        List fileList =
//                client.listObjects(bucketName, folderName).getObjectSummaries();
//        for (S3ObjectSummary file : fileList) {
//            client.deleteObject(bucketName, file.getKey());
//        }
//        client.deleteObject(bucketName, folderName);
//    }
}
Advertisements

Tomcat 7 JDBC Session Persistence

as root run:

export CATALINA_OPTS=”-Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true”

create table in mysql:

createtabletomcat_sessions (
  session_id     varchar(100) notnullprimarykey,
  valid_session  char(1) notnull,
  max_inactive   intnotnull,
  last_access    bigintnotnull,
  app_name       varchar(255),
  session_data   mediumblob,
  KEYkapp_name(app_name)
);
cd /usr/share/java
wget http://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar
cd /usr/share/tomcat7/lib and run:
/usr/share/tomcat7/lib# ln -s ../../java/mysql-connector.jar mysql-connector.jar
in META-INF create context.xml
paste Manager section:
<Context antiJARLocking="true" >
    <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
        virtualClasspath="/home/link/config"
        searchVirtualFirst="true" />

    <Manager className="org.apache.catalina.session.PersistentManager"
             maxIdleBackup="10">
        <Store className="org.apache.catalina.session.JDBCStore"
               connectionURL="jdbc:mysql://localhost/tomcat?user=root&amp;password=password"
               driverName="com.mysql.jdbc.Driver"
               sessionAppCol="app_name"
               sessionDataCol="session_data"
               sessionIdCol="session_id"
               sessionLastAccessedCol="last_access"
               sessionMaxInactiveCol="max_inactive"
               sessionTable="tomcat_sessions"
               sessionValidCol="valid_session" />
    </Manager>
</Context>

Run two projects on one tomcat in intellij idea

I have two web application in one project configured using pom. i want to run both the application simultaneously using same instance of Tomcat

Solution is:

Go to Run -> Edit configuration
* add a new configuration for your project (give it a jazzy name)
* Select your Application server( in your case tomcat {version} )
* on the server tab -> before launch section -> add build command for both your artifacts
* go to deployment tab -> add both your application war for deployment
* make sure you provide distinct application context (option on the right)
* Select you new configuration for your project
* click on that green play button .

Tomcat remote debuging

В начало startup.sh нужно добавить:

export JPDA_ADDRESS=8000
export JPDA_TRANSPORT=dt_socket

в конец (добавлено jpda):
exec “$PRGDIR”/”$EXECUTABLE” jpda start “$@”

В среде разработки нужно указать хост и порт(в данном случае 8000)

sha1 java

package main;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class HashTextTest {

/**

* @param args

* @throws NoSuchAlgorithmException

*/

public static void main(String[] args) throws NoSuchAlgorithmException {

System.out.println(sha1(“test string to sha1”));

}

static String sha1(String input) throws NoSuchAlgorithmException {

MessageDigest mDigest = MessageDigest.getInstance(“SHA1”);

byte[] result = mDigest.digest(input.getBytes());

StringBuffer sb = new StringBuffer();

for (int i = 0; i < result.length; i++) {

sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));

}

return sb.toString();

}

}

remove java in ubuntu

Remove all the Java related packages (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ):

sudo apt-get update
apt-cache search java | awk ‘{print($1)}’ | grep -E -e ‘^(ia32-)?(sun|oracle)-java’ -e ‘^openjdk-‘ -e ‘^icedtea’ -e ‘^(default|gcj)-j(re|dk)’ -e ‘^gcj-(.*)-j(re|dk)’ -e ‘java-common’ | xargs sudo apt-get -y remove
sudo apt-get -y autoremove
Purge config files:

dpkg -l | grep ^rc | awk ‘{print($2)}’ | xargs sudo apt-get -y purge
Remove Java config and cache directory:

sudo bash -c ‘ls -d /home/*/.java’ | xargs sudo rm -rf
Remove manually installed JVMs:

sudo rm -rf /usr/lib/jvm/*
Remove Java entries, if there is still any, from the alternatives:

for g in ControlPanel java java_vm javaws jcontrol jexec keytool mozilla-javaplugin.so orbd pack200 policytool rmid rmiregistry servertool tnameserv unpack200 appletviewer apt extcheck HtmlConverter idlj jar jarsigner javac javadoc javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc xulrunner-1.9-javaplugin.so; do sudo update-alternatives –remove-all $g; done
Search for possible remaining Java directories:

sudo updatedb
sudo locate -b ‘\pack200’
If the command above produces any output like /path/to/jre1.6.0_34/bin/pack200 remove the directory that is parent of bin, like this: sudo rm -rf /path/to/jre1.6.0_34.