~ubuntu-branches/ubuntu/vivid/jayatana/vivid

« back to all changes in this revision

Viewing changes to jayatanaag/src/main/java/com/jarego/jayatana/Agent.java

  • Committer: Package Import Robot
  • Author(s): Jared González
  • Date: 2014-12-10 17:38:15 UTC
  • Revision ID: package-import@ubuntu.com-20141210173815-8k4hm3bd481qkt63
Tags: upstream-2.7
Import upstream version 2.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2014 Jared González
 
3
 *
 
4
 * Permission is hereby granted, free of charge, to any
 
5
 * person obtaining a copy of this software and associated
 
6
 * documentation files (the "Software"), to deal in the
 
7
 * Software without restriction, including without limitation
 
8
 * the rights to use, copy, modify, merge, publish,
 
9
 * distribute, sublicense, and/or sell copies of the
 
10
 * Software, and to permit persons to whom the Software is
 
11
 * furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice
 
14
 * shall be included in all copies or substantial portions of
 
15
 * the Software.
 
16
 *
 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 
18
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 
19
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
20
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 
21
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
22
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
23
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 
24
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
25
 */
 
26
package com.jarego.jayatana;
 
27
 
 
28
import java.io.FileInputStream;
 
29
import java.lang.instrument.Instrumentation;
 
30
import java.util.logging.Level;
 
31
import java.util.logging.Logger;
 
32
 
 
33
/**
 
34
 * Este agente java permite evaluar si la arquitectura de las librerías nativas
 
35
 * de integración son de la misma arquitectura de la máquina virtual. En caso
 
36
 * de que conincidan inicia los procesos nativos de integración de lo contrario
 
37
 * solo se ignoran
 
38
 * 
 
39
 * @author Jared González
 
40
 */
 
41
public class Agent {
 
42
        /**
 
43
         * Evalua si la arquitectura de la librerías nativas de Jayatana coninciden con
 
44
         * la arquitectura de la máquia virutal de Java.
 
45
         * 
 
46
         * @param agentArgs argumentos de agente
 
47
         * @param inst Control de la intrumentación de Java
 
48
         */
 
49
        public static void premain(String agentArgs, Instrumentation inst) {
 
50
                try {
 
51
                        // excluir versiones 1.5 y 1.4 pues no esta soportadas
 
52
                        if (System.getProperty("java.version").startsWith("1.5") ||
 
53
                                        System.getProperty("java.version").startsWith("1.4"))
 
54
                                return;
 
55
                        
 
56
                        // obtienen el archivo de la librería nativa de integración
 
57
                        String libjayatanaag;
 
58
                        try {
 
59
                                if ((libjayatanaag = System.getenv("JAYATANA_LIBAGPATH")) == null) {
 
60
                                        libjayatanaag = "/usr/lib/jayatana/libjayatanaag.so";
 
61
                                } else {
 
62
                                        System.err.println("JAYATANA_LIBAGPATH="+libjayatanaag);
 
63
                                }
 
64
                        } catch (Exception e) {
 
65
                                libjayatanaag = "/usr/lib/jayatana/libjayatanaag.so";
 
66
                        }
 
67
                        
 
68
                        try {
 
69
                                // obtener arquitectura de la librería
 
70
                                String libjayatanaagarch = "--";
 
71
                                FileInputStream fis = new FileInputStream(libjayatanaag);
 
72
                                try {
 
73
                                        for (int i=0;i<4;i++)
 
74
                                                fis.read();
 
75
                                        if (fis.read() == 2)
 
76
                                                libjayatanaagarch = "64";
 
77
                                        else
 
78
                                                libjayatanaagarch = "32";
 
79
                                } finally {
 
80
                                        fis.close();
 
81
                                }
 
82
                                
 
83
                                // verificar si la arquitectura conicide con la máquina virtual
 
84
                                if (libjayatanaagarch.equals(System.getProperty("sun.arch.data.model")))
 
85
                                        System.load(libjayatanaag);
 
86
                        } catch (Exception e) {}
 
87
                } catch (Exception e) {
 
88
                        Logger.getLogger(Agent.class.getName())
 
89
                                .log(Level.WARNING, "can't load jayatana agent", e);
 
90
                }
 
91
        }
 
92
}