summaryrefslogtreecommitdiff
path: root/jasper_reports/JasperReports/jasper_server.py
blob: e6479d7d3a5385c3b9e9196eafd1d9a364e824df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2008-2012 NaN Projectes de Programari Lliure, S.L.
#                         http://www.NaN-tic.com
# Copyright (C) 2013 Tadeus Prastowo <tadeus.prastowo@infi-nity.com>
#                         Vikasa Infinity Anugrah <http://www.infi-nity.com>
# Copyright (C) 2019-Today Serpent Consulting Services Pvt. Ltd.
#                         (<http://www.serpentcs.com>)
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import os
import glob
import time
import socket
import subprocess
from xmlrpc import client as xmlrpclib
import logging

from odoo.exceptions import UserError
from odoo.tools.translate import _


class JasperServer:

    def __init__(self, port=8090):
        self.port = port
        self.pidfile = None
        self.javapath = None
        url = 'http://localhost:%d' % port
        self.proxy = xmlrpclib.ServerProxy(url, allow_none=True)
        self.logger = logging.getLogger(__name__)

    def error(self, message):
        if self.logger:
            self.logger.error("%s" % message)

    def path(self):
        return os.path.abspath(os.path.dirname(__file__))

    def start(self):
        logger = logging.getLogger(__name__)
        java_path = self.javapath
        logger.info("masuk method start jasper_server.py")
        logger.info(java_path)
        if java_path is False:
            raise UserError(_('Java Path Not Found !\n'
                              'Please add java path into the jasper '
                              'configuration page under the company form '
                              'view'))
        else:
            libraries = str(java_path) + '/lib'
            if os.path.exists(str(libraries)):
                self.javapath = java_path
            else:
                raise UserError(_('libraries Not Found !\n'
                                  'No libraries found in Java'))

        env = {}
        env.update(os.environ)
        if os.name == 'nt':
            a = ';'
        else:
            a = ':'
        libs = os.path.join(self.path(), '..', 'java', 'lib', '*.jar')
        env['CLASSPATH'] = os.path.join(self.path(), '..', 'java' + a) + \
            a.join(glob.glob(libs)) + a + os.path.join(
                self.path(), '..', 'custom_reports')

        cwd = os.path.join(self.path(), '..', 'java')

        # Set headless = True because otherwise, java may use
        # existing X session and if session is closed JasperServer
        # would start throwing exceptions. So we better avoid
        # using the session at all.
        command = ['java', '-Djava.awt.headless=true',
                   '-XX:MaxHeapSize=512m',
                   '-XX:InitialHeapSize=512m',
                   '-XX:CompressedClassSpaceSize=64m',
                   '-XX:MaxMetaspaceSize=128m',
                #    '-XX:+UseConcMarkSweepGC',   ### OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
                   'com.nantic.jasperreports.JasperServer',
                   str(self.port)]
        logger.info("ini command nya")
        logger.info(command)
        process = subprocess.Popen(command, env=env, cwd=cwd)
        logger.info("ini process nya")
        logger.info(process)
        logger.info("ini pid saat start")
        logger.info(self.pidfile)

        if self.pidfile:
            with open(self.pidfile, 'w') as f:
                f.write(str(process.pid))

    def execute(self, *args):
        """
        Render report and return the number of pages generated.
        """
        try:
            return self.proxy.Report.execute(*args)
        except socket.error as e:
            self.start()
            for x in range(40):
                time.sleep(1)
                try:
                    return self.proxy.Report.execute(*args)
                except socket.error as e:
                    self.error("EXCEPTION: %s %s" % (str(e), str(e.args)))
                    pass
                except xmlrpclib.Fault as e:
                    raise UserError(_('Report Error\n%s') % e)
        except xmlrpclib.Fault as e:
            raise UserError(_('Report Error\n%s') % e)