List of usage examples for java.lang.reflect InvocationHandler InvocationHandler
InvocationHandler
From source file:de.innovationgate.webgate.api.jdbc.WGDatabaseImpl.java
/** * @see de.innovationgate.webgate.api.WGDatabaseCore#open(WGDatabase, * String, String, String, boolean) *///from ww w . ja va 2s . co m public WGUserAccess open(WGDatabase db, String path, String user, String pwd, boolean prepareOnly) throws WGAPIException { try { this._db = db; this._path = path; this._aclImpl = new ACLImpl(this); String jdbcDriver = (String) db.getCreationOptions().get("Driver"); if (jdbcDriver == null) { jdbcDriver = (String) db.getCreationOptions().get("hibernate.connection.driver_class"); } // Determine dll version _csVersion = determineCSVersion(db, jdbcDriver, path, user, pwd); _ddlVersion = _csVersion.getVersion(); _fileHandling = createFileHandling(); boolean useSharedPool = WGUtils.getBooleanMapValue(db.getCreationOptions(), WGDatabase.COPTION_SHAREDPOOL, true); if (useSharedPool && db.getCreationOptions().containsKey(Database.OPTION_PATH) && db.getServer() instanceof SharedPoolJDBCDatabaseServer) { SharedPoolJDBCDatabaseServer poolServer = (SharedPoolJDBCDatabaseServer) db.getServer(); if (poolServer.isPoolAvailable(_csVersion)) { try { _connProvider = poolServer.createPoolConnectionProvider( (String) db.getCreationOptions().get(Database.OPTION_PATH)); WGFactory.getLogger() .info("Database '" + db.getDbReference() + "' uses the shared connection pool of database server '" + db.getServer().getTitle(Locale.getDefault()) + "'"); } catch (WGInvalidDatabaseException e) { throw e; } catch (Exception e) { throw new WGInvalidDatabaseException("Exception connecting to shared database server pool", e); } } } // Create regular connection provider if no shared one available/allowed if (_connProvider == null) { Properties props = new Properties(); if (path.startsWith("jdbc:")) { putDefaultConPoolProps(db, props); } if (user != null || pwd != null) { props.put("hibernate.connection.username", WGUtils.getValueOrDefault(user, "")); props.put("hibernate.connection.password", WGUtils.getValueOrDefault(pwd, "")); } String driverClass = (String) db.getCreationOptions().get("Driver"); props.put(Environment.ISOLATION, String.valueOf(Connection.TRANSACTION_READ_COMMITTED)); props.putAll(db.getCreationOptions()); try { _connProvider = new JDBCConnectionProvider(path, driverClass, props, true); } catch (JDBCConnectionException e) { throw new WGInvalidDatabaseException("Exception creating connection pool", e); } } // Build Session factory and builder buildSessionFactory(db, path, user, pwd, _csVersion, _connProvider); if ("true".equals(System.getProperty("de.innovationgate.wga.hibernate.enable_jmx"))) { _sessionFactory.getStatistics().setStatisticsEnabled(true); try { Object statisticsMBean = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { StatisticsMXBean.class }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(_sessionFactory.getStatistics(), args); } }); _jmxManager = new JmxManager(statisticsMBean, new ObjectName("de.innovationgate.WGAMonitor:name=Hibernate-Statistics,db=" + JmxManager.normalizeJmxKey(db.getDbReference()))); } catch (Exception e2) { WGFactory.getLogger().error("Exception enabling JMX for Hibernate statistics", e2); } } _sessionBuilder = _sessionFactory.withOptions(); // Determine save isolation _saveIsolationActive = _ddlVersion >= WGDatabase.CSVERSION_WGA5; if (db.getCreationOptions().containsKey("SaveIsolation")) { _saveIsolationActive = Boolean.parseBoolean((String) db.getCreationOptions().get("SaveIsolation")); } // parse masterPersistenceTimeout if (db.getCreationOptions().containsKey(COPTION_MASTERPERSISTENCE_TIMEOUT)) { _masterPersistenceTimeout = Long .parseLong((String) db.getCreationOptions().get(COPTION_MASTERPERSISTENCE_TIMEOUT)); } // parse HQL query default type String hqlType = (String) db.getCreationOptions().get(COPTION_HQL_FETCH_TYPE); if (hqlType != null) { _hqlLazyByDefault = hqlType.equals(HQL_FETCHTYPE_LAZY); } String hqlLazyParentCheck = (String) db.getCreationOptions().get(COPTION_HQL_LAZY_PARENTCHECK); if (hqlLazyParentCheck != null) { _hqlLazyParentCheck = Boolean.parseBoolean(hqlLazyParentCheck); } // open session WGUserAccess accessLevel; try { accessLevel = openSession(MasterLoginAuthSession.getInstance(), pwd, true); } catch (WGUnavailableException e) { throw new WGInvalidDatabaseException("Error opening initial session", e); } catch (WGBackendException e) { throw new WGInvalidDatabaseException("Error opening initial session", e); } if (accessLevel.getAccessLevel() <= WGDatabase.ACCESSLEVEL_NOACCESS) { try { close(); } catch (WGBackendException e1) { WGFactory.getLogger().error(e1); } } return accessLevel; } catch (WGInvalidDatabaseException e) { if (_connProvider != null) { if (_connProvider instanceof Stoppable) { ((Stoppable) _connProvider).stop(); } _connProvider = null; } throw e; } }
From source file:self.philbrown.droidQuery.$.java
/** * Registers change listeners for TextViews, EditTexts, and CompoundButtons. For all other * view types, this will trigger a function when the view's layout has been changed. * @param function the Function to call when the change event occurs. This will receive a * droidQuery instance for the changed view * @return this/* ww w. j a v a 2s .c o m*/ */ public $ change(final Function function) { for (int i = 0; i < this.views.size(); i++) { View view = this.views.get(i); final int index = i; if (view instanceof TextView) { ((TextView) view).addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { function.invoke($.with($.this.views.get(index))); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); } else if (view instanceof EditText) {//this is overkill, but what the hey ((EditText) view).addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { function.invoke($.with($.this.views.get(index))); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); } else if (view instanceof CompoundButton) { ((CompoundButton) view).setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { function.invoke($.with($.this.views.get(index))); } }); } else if (android.os.Build.VERSION.SDK_INT >= 11) { //default to size for API 11+ try { Class<?> eventInterface = Class.forName("android.view.View.OnLayoutChangeListener"); Method addOnLayoutChangeListener = view.getClass().getMethod("addOnLayoutChangeListener", new Class<?>[] { eventInterface }); InvocationHandler proxy = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { function.invoke($.with($.this.views.get(index))); return null; } }; Object eventHandler = Proxy.newProxyInstance(eventInterface.getClassLoader(), new Class<?>[] { eventInterface }, proxy); addOnLayoutChangeListener.invoke(view, eventInterface.cast(eventHandler)); } catch (Throwable t) { //unknown error } } } return this; }