terça-feira, 10 de novembro de 2015

ConcurrentModificationException

Referência: http://www.javacodegeeks.com/2011/05/avoid-concurrentmodificationexception.html


How to Avoid ConcurrentModificationException when using an Iterator

Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a ConcurrentModificationException.
This situation can come in case of multithreaded as well as single threaded environment.
Lets explore this scenario with the following example:
01import java.util.*;
02  
03public class IteratorExample {
04  
05    public static void main(String args[]){
06        List<String> myList = new ArrayList<String>();
07  
08        myList.add("1");
09        myList.add("2");
10        myList.add("3");
11        myList.add("4");
12        myList.add("5");
13  
14        Iterator<String> it = myList.iterator();
15        while(it.hasNext()){
16            String value = it.next();
17            System.out.println("List Value:"+value);
18            if(value.equals("3")) myList.remove(value);
19        }
20  
21        Map<String,String> myMap = new HashMap<String,String>();
22        myMap.put("1""1");
23        myMap.put("2""2");
24        myMap.put("3""3");
25  
26        Iterator<String> it1 = myMap.keySet().iterator();
27        while(it1.hasNext()){
28            String key = it1.next();
29            System.out.println("Map Value:"+myMap.get(key));
30            if(key.equals("2")){
31                myMap.put("1","4");
32                //myMap.put("4", "4");
33            }
34        }
35  
36    }
37}
Output is:
1List Value:1
2List Value:2
3List Value:3
4Exception in thread "main" java.util.ConcurrentModificationException
5    at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
6    at java.util.AbstractList$Itr.next(AbstractList.java:343)
7    at com.journaldev.java.IteratorExample.main(IteratorExample.java:27)
From the output stack trace, its clear that the exception is coming when we call iterator next() function. If you are wondering how Iterator checks for the modification, its implementation is present in AbstractList class where an int variable modCount is defined that provides the number of times list size has been changed. This value is used in every next() call to check for any modifications in a function checkForComodification().
Now comment the list part and run the program again.
Output will be:
1Map Value:3
2Map Value:2
3Map Value:4
Since we are updating the existing key value in the myMap, its size has not been changed and we are not getting ConcurrentModificationException. Note that the output may differ in your system because HashMap keyset is not ordered like list. If you will uncomment the statement where I am adding a new key-value in the HashMap, it will cause ConcurrentModificationException.
To Avoid ConcurrentModificationException in multi-threaded environment:
1. You can convert the list to an array and then iterate on the array. This approach works well for small or medium size list but if the list is large then it will affect the performance a lot.
2. You can lock the list while iterating by putting it in a synchronized block. This approach is not recommended because it will cease the benefits of multithreading.
3. If you are using JDK1.5 or higher then you can use ConcurrentHashMap and CopyOnWriteArrayList classes. It is the recommended approach.
To Avoid ConcurrentModificationException in single-threaded environment:
You can use the iterator remove() function to remove the object from underlying collection object. But in this case you can remove the same object and not any other object from the list.
Let us run an example using Concurrent Collection classes:
01package com.journaldev.java;
02  
03import java.util.Iterator;
04import java.util.List;
05import java.util.Map;
06import java.util.concurrent.ConcurrentHashMap;
07import java.util.concurrent.CopyOnWriteArrayList;
08  
09public class ThreadSafeIteratorExample {
10  
11    public static void main(String[] args) {
12  
13        List<String> myList = new CopyOnWriteArrayList<String>();
14  
15        myList.add("1");
16        myList.add("2");
17        myList.add("3");
18        myList.add("4");
19        myList.add("5");
20  
21        Iterator<String> it = myList.iterator();
22        while(it.hasNext()){
23            String value = it.next();
24            System.out.println("List Value:"+value);
25            if(value.equals("3")){
26                myList.remove("4");
27                myList.add("6");
28                myList.add("7");
29            }
30        }
31        System.out.println("List Size:"+myList.size());
32  
33        Map<String,String> myMap =
34             new ConcurrentHashMap<String,String>();
35        myMap.put("1""1");
36        myMap.put("2""2");
37        myMap.put("3""3");
38  
39        Iterator<String> it1 = myMap.keySet().iterator();
40        while(it1.hasNext()){
41            String key = it1.next();
42            System.out.println("Map Value:"+myMap.get(key));
43            if(key.equals("1")){
44                myMap.remove("3");
45                myMap.put("4""4");
46                myMap.put("5""5");
47            }
48        }
49  
50        System.out.println("Map Size:"+myMap.size());
51    }
52  
53}
Output is:
01List Value:1
02List Value:2
03List Value:3
04List Value:4
05List Value:5
06List Size:6
07Map Value:1
08Map Value:null
09Map Value:4
10Map Value:2
11Map Size:4
From the above example its clear that:
1. Concurrent Collection classes can be modified avoiding ConcurrentModificationException.
2. In case of CopyOnWriteArrayList, iterator doesn’t accomodate the changes in the list and works on the original list.
3. In case of ConcurrentHashMap, the behavior is not always the same.
For condition:
1if(key.equals("1")){
2    myMap.remove("3");
Output is:
1Map Value:1
2Map Value:null
3Map Value:4
4Map Value:2
5Map Size:4
It is taking the new object added with key “4? but not the next added object with key “5?.
Now if I change the condition to
1if(key.equals("3")){
2    myMap.remove("2");
Output is:
1Map Value:1
2Map Value:3
3Map Value:null
4Map Size:4
In this case its not considering the new added objects.
So if you are using ConcurrentHashMap then avoid adding new objects as it can be processed depending on the keyset. Note that the same program can print different values in your system because HashMap keyset is not in any order.
Extra Toppings:
1for(int i = 0; i<myList.size(); i++){
2    System.out.println(myList.get(i));
3    if(myList.get(i).equals("3")){
4        myList.remove(i);
5        i--;
6        myList.add("6");
7    }
8}
If you are working on single-threaded environment and want your code to take care of the extra added objects in the list then you can do so using following code and avoiding iterator.
Note that I am decreasing the counter because I am removing the same object, if you have to remove the next or further far object then you don’t need to decrease the counter.
Try it yourself.

quinta-feira, 29 de outubro de 2015

Inserir imagem em rich:tab



<rich:tab styleClass="background" header="Lista" id="tabPesquisa">
<f:facet name="header">
   <h:panelGroup>      
       <h:outputLabel value="Lista  "></h:outputLabel>
       <h:graphicImage value="/resources/images/16x16/doc2.png"/>
   </h:panelGroup>
</f:facet>
</rich:tab>

sexta-feira, 9 de outubro de 2015

Configurando o serviço de VPN no Windows 2003 (Client-To-Site)


http://www.techrepublic.com/article/configure-a-windows-server-2003-vpn-on-the-server-side/

http://www.techrepublic.com/article/configure-a-windows-server-2003-vpn-on-the-server-side/

Resolvendo o problema da rota padrão 0.0.0.0
https://social.technet.microsoft.com/Forums/pt-BR/11eae0c3-6479-4dd8-8ee6-2bba613094da/vpn-acesso-rede-interna?forum=arquiteturainfrapt



Configurar a conexão no cliente:





Configurações do Firewall:

1) Direcionar a porta 1723/TCP para o Windows 2003 rodando a VPN.
2) Direcionar a porta 47/GRE para o Windows 2003 rodando a VPN.

Recursos adicionais:
http://blogs.technet.com/b/latam/archive/2006/11/17/problemas-de-roteamento-ao-acessar-recursos-atrav-s-de-uma-vpn.aspx

http://www.isaserver.org/img/upl/vpnkitbeta2/cmak.htm

segunda-feira, 5 de outubro de 2015

Configurando OpenVpn no Windows

Versão utilizada: 2.3.8-I601-i686 (Problemas na versão 64-bit)

1) Seguir o procedimento informado no site do fabricante.

https://community.openvpn.net/openvpn/wiki/Easy_Windows_Guide#DownloadingandInstallingOpenVPN

Preparatory Steps

  1. Navigate to the C:\Program Files\OpenVPN\easy-rsa folder in the command prompt:
    1. Press Windows Key + R
    2. Type "cmd.exe" and press Enter.
      cmd.exe
      
    3. Navigate to the correct folder:
      cd "C:\Program Files\OpenVPN\easy-rsa"
      
  1. Initialize the OpenVPN configuration:
    init-config
    
    • NOTE: Only run init-config once, during installation.
  1. Open the vars.bat file in a text editor:
    notepad vars.bat
    
  1. Edit the following lines in vars.bat, replacing "US", "CA," etc. with your company's information:
    set KEY_COUNTRY=US
    set KEY_PROVINCE=CA
    set KEY_CITY=SanFrancisco
    set KEY_ORG=OpenVPN
    set KEY_EMAIL=mail@host.domain
    
  1. Save the file and exit notepad.
  1. Run the following commands:
    vars
    
    clean-all
    

Building Certificates and Keys

  1. The certificate authority (CA) certificate and key:
    build-ca
    
    • When prompted, enter your country, etc. These will have default values, which appear in brackets. For your "Common Name," a good choice is to pick a name to identify your company's Certificate Authority. For example, "OpenVPN-CA":
      Country Name (2 letter code) [US]:
      State or Province Name (full name) [CA]:
      Locality Name (eg, city) [SanFrancisco]:
      Organization Name (eg, company) [OpenVPN]:
      Organizational Unit Name (eg, section) []:
      Common Name (eg, your name or your server's hostname) []:OpenVPN-CA
      Email Address [mail@host.domain]:
      
  1. The server certificate and key:
    build-key-server server
    
    • When prompted, enter the "Common Name" as "server"
    • When prompted to sign the certificate, enter "y"
    • When prompted to commit, enter "y"
  1. Client certificates and keys:
  1. For each client, choose a name to identify that computer, such as "mike-laptop" in this example.
    build-key mike-laptop
    
    • When prompted, enter the "Common Name" as the name you have chosen (e.g. "mike-laptop")
  2. Repeat this step for each client computer that will connect to the VPN.
  1. Generate Diffie Hellman parameters (This is necessary to set up the encryption)
    build-dh
    

Configuration Files

  1. Find the sample configuration files:
    Start Menu -> All Programs -> OpenVPN -> OpenVPN Sample Configuration Files
    

Server Config File

  1. Open server.ovpn
  1. Find the following lines:
    ca ca.crt
    cert server.crt
    key server.key
    
    dh dh1024.pem
    
  1. Edit them as follows:
    ca "C:\\Program Files\\OpenVPN\\config\\ca.crt"
    cert "C:\\Program Files\\OpenVPN\\config\\server.crt"
    key "C:\\Program Files\\OpenVPN\\config\\server.key"
    
    dh "C:\\Program Files\\OpenVPN\\config\\dh1024.pem"
    
  1. Save the file as C:\Program Files\OpenVPN\easy-rsa\server.ovpn

Client Config Files

This is similar to the server configuration
  1. Open client.ovpn
  1. Find the following lines:
    ca ca.crt
    cert client.crt
    key client.key
    
  1. Edit them as follows:
    ca "C:\\Program Files\\OpenVPN\\config\\ca.crt"
    cert "C:\\Program Files\\OpenVPN\\config\\mike-laptop.crt"
    key "C:\\Program Files\\OpenVPN\\config\\mike-laptop.key"
    
    • Notice that the name of the client certificate and key files depends upon the Common Name of each client.
  1. Edit the following line, replacing "my-server-1" with your server's public Internet IP Address or Domain Name. If you need help, see Static Internet IPbelow.
    remote my-server-1 1194
    
  1. Save the file as C:\Program Files\OpenVPN\easy-rsa\mike-laptop.ovpn (in this example. Each client will need a different, but similar, config file depending upon that client's Common Name.)

Copying the Server and Client Files to Their Appropriate Directories

  1. Copy these files from C:\Program Files\OpenVPN\easy-rsa\ to C:\Program Files\OpenVPN\config\ on the server:
    ca.crt
    dh1024.pem
    server.crt
    server.key
    server.ovpn
    
  1. Copy these files from C:\Program Files\OpenVPN\easy-rsa\ on the server to C:\Program Files\OpenVPN\config\ on each client (mike-laptop, in this example):
    ca.crt
    mike-laptop.crt
    mike-laptop.key
    mike-laptop.ovpn
    

Starting OpenVPN

  1. On both client and server, run OpenVPN from:
    Start Menu -> All Programs -> OpenVPN -> OpenVPN GUI
    
  1. Double click the icon which shows up in the system tray to initiate the connection. The resulting dialog should close upon a successful start.

Further Considerations / Troubleshooting

Firewall Configuration

If you have connection problems, make sure to set a rule on your server's firewall allowing incoming traffic on UDP port 1194.

Port Forwarding

If your server is behind a router, you will need to forward the port chosen for OpenVPN (in this example UDP 1194) to the server. Consult your router's documentation for details on this.
To set up port forwarding, you will likely need to set up the server with a static local IP address instead of the default dynamic (changing) IP. Instructions for Windows XP may be found here. Make sure to choose a static IP address that is not in the range your router might assign as a dynamic IP, but is within the router's subnet (usually 192.168.0.xxx , 10.0.0.xxx , or similar).

Static Internet IP

Your server will need to have a static internet IP or Domain Name to be accessible over the long term. One solution is to sign up for an account with DynDNS and install the DynDNS Updater on your server. When signing up you will determine the static Domain Name of your server. (For example, "myserver.dyndns.org") You will use this Domain Name in the client configuration files as part of the "remote" directive.

Running OpenVPN as a Service

Running OpenVPN as a service will allow:
  1. OpenVPN to be run from a non-administrator account.
  1. OpenVPN to be started automatically on system startup. This is often preferred on the server machine, as well as any machines which will be constantly connected to the server.
  1. Run the Windows Service administrative tool:
    1. Press Windows Key + R
    2. Type "services.msc" and press Enter.
      services.msc
      
  1. Find the OpenVPN service, and set its Startup Type to "automatic."
  1. Optionally, start the service now.

Security Tips

  1. Transmit all needed files to the client computers using a secure means such as a USB drive (email is not always a secure means).
  2. Choose a port other than UDP 1194, and replace the port number wherever this guide mentions UDP port 1194.

Cloning OpenVPN Servers

If including OpenVPN in a cloned server build you will find that all servers will have the same MAC address for the TAP device. This will cause packet loss across the network. Standard methods of changing the IP address from scripts do not work on the TAP device, to resolve this delete and recreate the TAP device using the scripts included with OpenVPN:
C:\Program Files\OpenVPN\bin\deltapall
C:\Program Files\OpenVPN\bin\addtap
You will then have to rename the connection to match the entry in the config file.


2) Configurar o router para encaminhar os pacotes UDP da porta 1194 para a maquina que estiver rodando o openVpn.

3)Compartilhar acesso a rede interna
Para compartilhar o acesso a rede é necessário ativar o compartilhamento na placa de rede do Windows.

4) Possíveis erros
4.1) ERROR: netsh command failed: returned error code 1
4.1) Se ocorrer esse erro você deve renomear as conexões de rede envolvidas removendo 
4.1) os espaços em branco (Ex: De Local Area  Network Para LocalAreaNetwork).
4.1) Ref: https://vpn.ccrypto.org/page/install-windows

5) O mais importante: Revogar certificados
5.1) Entrar no diretorio C:\Program Files (x86)\OpenVPN\easy-rsa
5.2) Executar: vars
5.3) Executar: revoke-full nome-certificado (ex: reunicao-note)
5.4) Revogado com sucesso se aparecer: error 23 at 0 depth lookup:certificate revoked
5.5) Crie o diretório revogado C:\Program Files (x86)\OpenVPN\easy-rsa\keys\revogados.
5.6) Mova os arquivos referentes ao certificado revogado para o diretorio revogados.
5.7) Adicione a linha abaixo no server.ovpn para que o oVpn leia o arquivo que contem as informações dos certificados revogados.
crl-verify "C:\\Program Files (x86)\\OpenVPN\\easy-rsa\\keys\\crl.pem"
5.8) Efetue o restar do oVpn.
5.9) Ref: http://kb.3gnt.net/kb/entry/31/ http://www.hardware.com.br/tutoriais/openvpn_2/pagina5.html

Obs:
Ambiente de teste:
Servidor = Windows 7 64Bits
Cliente = Windows Vista 3Bits
Firewall = BRMA

sexta-feira, 18 de setembro de 2015

[oracle] Tablespace de UNDO muito grande

[oracle] Tablespace de UNDO muito grande

8012010
Ontem tive um problema com um banco de dados, o banco não conseguia estender mais espaço para a tablespace de UNDO, pois, a mesma havia chego no seu tamanho máximo.
Mesmo parando todas as sessões do BD a tablespace não diminuia, então pesquisando um pouco verifiquei que a tablespace de UNDO possui um tempo de retenção de dados, na qual é utilizado para busca de informações do passado (informações já commitadas),  como a funcionalidade de flashback do Oracle.
Como verificar o melhor tempo de retenção
Você pode determinar o período de retenção por meio de consulta a coluna TUNED_UNDORETENTION da visão V $ UNDOSTAT. Esta exibição contém uma linha para cada 10 minutos de intervalo de coleta de estatísticas, nos últimos 4 dias (TUNED_UNDORETENTION é dado em segundos).
select
     to_char(begin_time, 'DD-MON-RR HH24:MI')
     begin_time,
     to_char(end_time, 'DD-MON-RR HH24:MI')
     end_time, tuned_undoretention
 from v$undostat
 order by end_time;
BEGIN_TIMEEND_TIMETUNED_UNDORETENTION
08-JAN-10 09:1508-JAN-10 09:25600
08-JAN-10 09:2508-JAN-10 09:35600
08-JAN-10 09:3508-JAN-10 09:45600
08-JAN-10 09:4508-JAN-10 09:55600
08-JAN-10 09:5508-JAN-10 10:05600

Especificando o tempo de retenção
Para especificar o tempo de retenção pelo atributo você pode mudar o atributoUNDO_RETENTION no arquivo de parâmetros de inicialização do BD.
Para mudar o tempo de retenção a qualquer momento basta executar o seguinte comando:
ALTER SYSTEM SET UNDO_RETENTION = 600;
A mudança ocorrerá imediatamente, mas só será efetivamente aplicada se a tablespace de UNDO possuir espaço sufiente.
Limpando os dados da tablespace de UNDO
No problema  (tablespace de UNDO estava muito grande)  havia um agravante de  limitação de espaço, uma vez que este cliente estava utilizando o Oracle XE.
Então foi necessário limpar a tablespace de UNDO, pesquisando um pouco descobri que isso só é possível criando uma nova tablespace de UNDO e apagando a velha com seus dados. Para efetuar esse processo é necessário executar os seguintes comandos:
-- Cria uma nova tablespace
create undo tablespace undotbs02 datafile 'C:\ORACLEXE\ORADATA\XE\UNDO02.DBF'
size 400M autoextend on next 50m maxsize 700M;

-- especifica para o banco de dados a nova tablespace de UNDO
alter system set undo_tablespace='undotbs02';

-- apaga a antiga tablespace de UNDO
drop tablespace UNDO including contents and datafiles;
Referencia:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14231/undo.htm
https://ldiasrs.wordpress.com/2010/01/08/oracle-tablespace-de-undo-muito-grande/