Friday, March 1, 2019
Wednesday, November 27, 2013
iumls
Labels:
computer,
grammar,
java,
language,
open-source,
parser,
syntaxm project
Wednesday, November 20, 2013
how to find large and last modified files on linux?
I decided to make a short note about how to find file which were recently modifed and also they have a quite big size.
It's to find the cause of disk overfill.
find /var -type f -size +100M -exec stat --format '%Y :%y %n' {} \; | sort -nr | cut -d: -f2- | head
solution has been got from http://stackoverflow.com/questions/5566310/how-to-recursively-find-and-list-the-latest-modified-files-in-a-directory-with-s .
It's to find the cause of disk overfill.
find /var -type f -size +100M -exec stat --format '%Y :%y %n' {} \; | sort -nr | cut -d: -f2- | head
solution has been got from http://stackoverflow.com/questions/5566310/how-to-recursively-find-and-list-the-latest-modified-files-in-a-directory-with-s .
Monday, March 18, 2013
Java, Trac, RPC and JSON
I thought it would be great (and maybe useful for you) to publish here my experience how to set up Trac with RPC over JSON. When I tried to do it from the scratch I had realized that a lot of things work in unexpected way, at least for me.
The main goal is to manage Trac tickets with some application. Here you'll get all information about plugin http://trac-hacks.org/wiki/XmlRpcPlugin. I want to make some details clear for you and show you my ready/worked code.
Ok. Short list of items.
The main goal is to manage Trac tickets with some application. Here you'll get all information about plugin http://trac-hacks.org/wiki/XmlRpcPlugin. I want to make some details clear for you and show you my ready/worked code.
Trac and Apache
Ok. Short list of items.
- install trac;
- install plugin 'TracXMLRPC';
- enable plugin (tracrpc.* = enabled in trac.ini, see below);
- set up web server with authorization;
- create user with 'XML_RPC' privileges in trac;
- it's really simple, use Basic Authorization (see below green pieces of text);
- create user ('htpasswd') and grant its to 'XML_PRC' (
trac-admin /path/to/projenv permission add user_name XML_PRC
); - Also, you maybe would be interested to set up mixed Authorization (fixed files + LDAP), see orange pieces of text below);
- In Java (see below):
- do auth;
- create JSON;
- send and interpret.
Apache configuration
<IfModule wsgi_module> WSGIScriptAlias /trac /path_to_trac/trac_site/cgi-bin/trac.wsgi WSGIPassAuthorization On <Directory /path_to_trac/trac_site/cgi-bin> WSGIApplicationGroup %{GLOBAL} Require all granted AllowOverride None Order allow,deny Allow from all </Directory> <Location /commref_req_trac/login> AuthBasicProvider file ldap AuthType Basic #AuthzLDAPAuthoritative off AuthUserFile /path_to_trac/trac_env/passwd_file AuthName "Trac" AuthLDAPURL "ldap://domain.com:389/dc=domain,dc=com?sAMAccountName?sub?(objectClass=user)" AuthLDAPBindDN "CN=user_ldap,OU=Service Accounts,OU=GROUP,DC=domain,DC=com" AuthLDAPBindPassword "user_ldap_password" Require valid-user </Location> </IfModule>
Trac configuration
[components]
tracrpc.* = enabled
Java
Transport
How to interact with Trac. For JSON I used library 'org.json' ('https://github.com/douglascrockford/JSON-java') and 'Apache Commons Codec' ('http://commons.apache.org/proper/commons-codec/') for Base64.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import org.apache.commons.codec.binary.Base64; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class TracAPI { private static int jsonIdSequence = 1; public static JSONObject sendJsonRpc(String method, JSONArray parameters) throws IOException, JSONException { JSONObject rpc = new JSONObject(); rpc.put("method", method); rpc.put("params", parameters); rpc.put("id", jsonIdSequence++); String tracUrl = Configuration.getTracApiUrl(); String tracUsername = Configuration.getTracApiUsername(); String tracPassword = Configuration.getTracApiPassword(); URL url = new URL(tracUrl); URLConnection conn = url.openConnection(); String userpass = tracUsername + ":" + tracPassword; String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes())); conn.setRequestProperty ("Authorization", basicAuth); conn.setRequestProperty ("Content-Type", "application/json"); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); Logger.debug("jsonrpc request:\n" + rpc.toString()); wr.write(rpc.toString()); wr.flush(); wr.close(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; String jsonText = ""; while ((line = rd.readLine()) != null) { jsonText += line; } rd.close(); Logger.debug("jsonrpc response:\n" + jsonText); return new JSONObject(jsonText); } }
Payload
read ticket
JSONArray parameters = new JSONArray(); parameters.put(ticketId); //id JSONObject resp = TracAPI.sendJsonRpc("ticket.get", parameters); String status = resp.getJSONArray("result").getJSONObject(3).getString("status"); String resolution = resp.getJSONArray("result").getJSONObject(3).getString("resolution"); String owner = resp.getJSONArray("result").getJSONObject(3).getString("owner");
create ticket
JSONArray parameters = new JSONArray(); parameters.put("summary text"); //summary parameters.put("description text"); //description parameters.put(new JSONObject()); //attributes parameters.put(false); //notify JSONObject resp = TracAPI.sendJsonRpc("ticket.create", parameters);
update/modify ticket
Update must be performed in two stages:- Determine (current) version (timestamp) of ticket (to protect ticket from concurrent modifications, you have to indicate which version of ticket you're modifying);
- Perform modification.
Determine timestamp.
JSONArray parameters = new JSONArray(); parameters.put(ticketId); //id JSONObject resp = TracAPI.sendJsonRpc("ticket.get", parameters); long ts = resp.getJSONArray("result").getJSONObject(3).getLong("_ts");
Update.
JSONObject attrs = new JSONObject(); attrs.put("action", "accept"); attrs.put("_ts", ts); parameters = new JSONArray(); parameters.put(ticketId); // id parameters.put("comment text"); // comment parameters.put(attrs); // attributes parameters.put(false); // notify resp = TracAPI.sendJsonRpc("ticket.update", parameters);
Feel free to write comments if you have questions/suggestions or another stuff you'd like to share. Thank you.
Java code was highlighted with "http://markup.su/highlighter/".
Tuesday, September 20, 2011
LDAP/AD user password check
Simple algorithm.
highlighted with: http://tohtml.com/
- connect to ldap;
- auth with "super"-user (user1): special user with known DN and password;
- search another user (user2): user who must be tested, search him with some attribute (for example
sAMAccountName); - determine DN of user;
- open one more connection to ldap with just found DN and password of user2;
- no exception - ok, otherwise user2 has entered wrong password.
/** * Returns {@link Item} with addition information in case of user exists and authicated correctly, otherwise null * @param username username * @param password password * @return Item or null */ private Item checkWithLdap(String username, String password) { String ldap = CXConfiguration.getParameterAsString("LDAP_SERVER"); if ((ldap != null) && !ldap.isEmpty()) { DirContext ctx = null; try { int port = CXConfiguration.getParameterAsInt("LDAP_SERVER"); String ldapUrl = "ldap://" + ldap + ":" + port; Logger.info("checking with ldap [username: " + username + ", password: ******, ldap: " + ldapUrl + "]"); Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put( Context.SECURITY_PRINCIPAL, CXConfiguration.getParameterAsString("LDAP_AUTH_DN") ); env.put( Context.SECURITY_CREDENTIALS, CXConfiguration.getParameterAsString("LDAP_AUTH_PASSWORD") ); ctx = new InitialDirContext(env); String base = CXConfiguration.getParameterAsString("LDAP_BASE_DN"); String[] attributeFilter = { CXConfiguration.getParameterAsString("LDAP_USER_SEARCH_ATTR"), "distinguishedName", CXConfiguration.getParameterAsString("LDAP_EMAIL_ATTR") }; SearchControls sc = new SearchControls(); sc.setReturningAttributes(attributeFilter); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(" + CXConfiguration.getParameterAsString("LDAP_USER_SEARCH_ATTR") + "=" + username + ")"; NamingEnumeration<SearchResult> results = ctx.search(base, filter, sc); if (results.hasMore()) { Properties env1 = new Properties(); env1.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env1.put(Context.PROVIDER_URL, ldapUrl); SearchResult result = (SearchResult) results.next(); Attributes attrs = result.getAttributes(); Attribute dnAttr = attrs.get("distinguishedName"); String dn = (String) dnAttr.get(); Attribute emailAttr = attrs.get(CXConfiguration.getParameterAsString("LDAP_EMAIL_ATTR")); String email = (emailAttr.get() != null) ? (String) emailAttr.get() : ""; env1.put(Context.SECURITY_PRINCIPAL, dn); env1.put(Context.SECURITY_CREDENTIALS, password); new InitialDirContext(env1); // in case of problem exception will be threw Item i = new Item(); i.setValue("email", email); Logger.info("checking with ldap - ok"); return i; } else return null; } catch (Exception e) { Logger.error("checking with ldap - ldap error", e); return null; } finally { if (ctx != null) try { ctx.close(); } catch (NamingException e) { } } } else return null; }
highlighted with: http://tohtml.com/
Хорошая статья "Безопастность в Java"
Хорошая статья на русском языке о безопастности в Java. В двух словах, доступно, просто и понятно. Отличная статья для старта обзора этой области.
http://voituk.kiev.ua/2008/08/18/bezopastnost-v-java/
http://voituk.kiev.ua/2008/08/18/bezopastnost-v-java/
Tuesday, September 6, 2011
GSM 7bit (part of PDU) pack/encoding algorithm in Java
GSM 7bit (part of PDU) pack/encoding algorithm in Java. This can be used for SMS/USSD writing.
public static String stringTo7bit(String string) { String hex = ""; byte[] bytes = string.getBytes(); int f = 0; while (f < bytes.length - 1) { int t = (f % 8) + 1; if (t < 8) { byte b = (byte) (((bytes[f] >>> (t - 1)) | (bytes[f + 1] << (8 - t))) & 0x000000FF); hex += intToHex(b & 0x000000FF).toUpperCase(); } f++; } if ((f % 8) + 1 < 8) hex += intToHex((bytes[f] >>> (f % 8)) & 0x000000FF).toUpperCase(); return hex; }
public static String intToHex(int i) { String hex = Integer.toHexString(i); if (hex.length() % 2 != 0) hex = 0 + hex; return hex; }
highlighted with: http://tohtml.com/
Tuesday, June 7, 2011
Взаимодействие между потоками в Java. Wait и notify. | Блог еще одного разработчика
Взаимодействие между потоками в Java. Wait и notify. | Блог еще одного разработчика
Отличная статья на русском языке.
К сожалению, исходная статья более не доступна по ссылке.
Привожу копию из архивов с ресурса archive-ru.com.
Исходная статья с сайта http://stremoukhov.ru.
Отличная статья на русском языке.
К сожалению, исходная статья более не доступна по ссылке.
Привожу копию из архивов с ресурса archive-ru.com.
Исходная статья с сайта http://stremoukhov.ru.
Взаимодействие между потоками в Java. Wait и notify.
Представим ситуацию, что у нас многопотоковое приложение, написанное на Java. Есть некий класс, выполняющий какое-либо конкретное действие с данными, например, отправку их по почте, а сами данные подготавливаются в другом месте кода в другом потоке. Перед отправкой данных нам необходимо как-то связаться с потоком, подготавливающим данные, дабы поймать момент, когда они будут готовы.
Пример 1. Простой, но неправильный пример
public class DataManager {
private static boolean ready = false;
public void sendData() {
while (!ready) {
// waiting
System.out.println("Waiting for data...");
}
// continue execution and sending data
System.out.println("Sending data...");
}
public void prepareData() {
System.out.println("Data prepared");
ready = true;
}
}
private static boolean ready = false;
public void sendData() {
while (!ready) {
// waiting
System.out.println("Waiting for data...");
}
// continue execution and sending data
System.out.println("Sending data...");
}
public void prepareData() {
System.out.println("Data prepared");
ready = true;
}
}
Допустим, что первый поток должен готовить данные, а второй отправлять. Из данного примера видно, что отправляющий поток будет ждать данные в цикле. Это, конечно, будет работать, особенно, если время ожидание мизерное. Но по сути, ожидая данных поток серьезно загрузит процессор, отнимая драгоценные ресурсы на подготовку данных. Правильнее будет каким-то образом приостановить второй поток, пока первый не подготовит данные.
Пример 2. Ожидание с помощью wait() и notifyAll()
public class DataManager {
private static final Object monitor = new Object();
public void sendData() {
synchronized (monitor) {
System.out.println("Waiting for data...");
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
// continue execution and sending data
System.out.println("Sending data...");
}
}
public void prepareData() {
synchronized (monitor) {
System.out.println("Data prepared");
monitor.notifyAll();
}
}
}
private static final Object monitor = new Object();
public void sendData() {
synchronized (monitor) {
System.out.println("Waiting for data...");
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
// continue execution and sending data
System.out.println("Sending data...");
}
}
public void prepareData() {
synchronized (monitor) {
System.out.println("Data prepared");
monitor.notifyAll();
}
}
}
Как видно из данного примера, отправляющий данные поток не циклиться, а вызывает метод wait() у нашего объекта-монитора. Напомню принцип работы синхронизации: как только первый поток попадает в блок synchronized, он становится владельцем объекта monitor и "блокирует" его. Никакой другой поток не сможет попасть в блок, т.е. стать владельцем этого объекта до тех пор, пока поток владелец не "отпустит" монитор, выйдя из блока синхронизации.
Важно! Методы wait(), notify() и notifyAll() должны обязательно находиться внутри блока synchronized, либо внутри synchronized-метода, иначе вы получите Exception. Но возникает вопрос, каким образом второй поток сможет попасть в блок синхронизации, чтобы вызвать notifyAll(), если первый в этот момент будет владельцем monitor'а и будет ждать notify() ? Все просто - как только поток достигает метода wait() он перестает быть владельцем монитора, блокировка снимается, а поток уходит в сон.
Потенциальные проблемы данного кода:
1. Возможна ситуация, когда первый поток подготовил данные раньше, чем второй начал ждать его. В этом случае notifyAll() никого не разбудит и соответственно, когда второй поток дойдет до wait(), процесс остановится навсегда.
2. Существуют в природе так называемые "ложные пробуждения", когда поток просыпается сам по себе, без всяких на то причин.
1. Возможна ситуация, когда первый поток подготовил данные раньше, чем второй начал ждать его. В этом случае notifyAll() никого не разбудит и соответственно, когда второй поток дойдет до wait(), процесс остановится навсегда.
2. Существуют в природе так называемые "ложные пробуждения", когда поток просыпается сам по себе, без всяких на то причин.
Чтобы избежать обе проблемы, усовершенствуем наш код.
Пример 3. Ожидание в цикле.
Пример 3. Ожидание в цикле.
public class DataManager implements Runnable {
private static final Object monitor = new Object();
private static boolean ready = false;
public void prepareData() {
synchronized (monitor) {
System.out.println("Data prepared");
ready = true;
monitor.notifyAll();
}
}
public void sendData() {
synchronized (monitor) {
System.out.println("Waiting for data...");
while (!ready) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// continue execution and sending data
System.out.println("Sending data...");
}
}
}
private static final Object monitor = new Object();
private static boolean ready = false;
public void prepareData() {
synchronized (monitor) {
System.out.println("Data prepared");
ready = true;
monitor.notifyAll();
}
}
public void sendData() {
synchronized (monitor) {
System.out.println("Waiting for data...");
while (!ready) {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// continue execution and sending data
System.out.println("Sending data...");
}
}
}
Не смотря на цикл как в примере 1, здесь у нас не будет расхода ресурсов процессора, т.к. цикл повторится только в случае ложного пробуждения, или прерывания. Также, мы не попадем в цикл, а следовательно на метод wait() если данные уже готовы на момент исполнения sendData();
Thursday, April 14, 2011
an idea box
My exclusive resource is available for your on alpha-stage. Try to invent something and earn money.
http://scaleus.palettelabs.org
http://scaleus.palettelabs.org
Monday, February 28, 2011
Synchronize PL/SQL sources from ORACLE with SVN repository
Small utility published, now you can synchronize PL/SQL code from ORACLE DBMS with SVN repository.
https://sourceforge.net/projects/dbcode-svn-sync/
https://sourceforge.net/projects/dbcode-svn-sync/
Friday, September 3, 2010
split function in ORACLE
http://klauskurz.wordpress.com/2010/06/04/oracle-split-function/
QUOTE:
This function splits a string by the given regex in Oracle 11g
QUOTE:
This function splits a string by the given regex in Oracle 11g
-- type CREATE OR REPLACE TYPE str_array AS TABLE OF VARCHAR2(1024 CHAR); -- code CREATE OR REPLACE FUNCTION SPLIT( i_str IN VARCHAR2, i_regex IN VARCHAR2, i_modifier IN VARCHAR2 DEFAULT NULL) RETURN str_array IS l_str_array str_array; l_count NUMBER; BEGIN l_count := regexp_count(i_str, i_regex, 1, i_modifier); SELECT CAST( MULTISET ( SELECT CASE WHEN l_count = 0 THEN i_str WHEN LEVEL = l_count + 1 THEN regexp_substr( i_str, '.*(' || i_regex || ')(.*)', 1, 1, i_modifier, 2) ELSE regexp_substr( i_str, '(.*?)(' || i_regex || ')', 1, LEVEL, i_modifier, 1) END AS SPLIT FROM dual CONNECT BY LEVEL <= l_count + 1) AS str_array) INTO l_str_array FROM dual; RETURN l_str_array; END;highlighted with: http://tohtml.com/
Friday, July 23, 2010
Tuesday, May 4, 2010
how to use proxy with Apache HttpClient?
Small pin.
public static connect() { // some code ... HttpClient client = new HttpClient(); client.getHostConfiguration().setProxy( "PROXY-SERVER", proxyPort); client.getState().setProxyCredentials( AuthScope.ANY, new UsernamePasswordCredentials("USERNAME", "PASSWORD")); // some code ... }highlighted with: http://tohtml.com/
Subscribe to:
Posts (Atom)